### Python Bindings Installation Source: https://context7.com/cozis/tinytemplate/llms.txt Details the steps required to build and install the Python extension module for TinyTemplate. It covers using `setup.py` for building and installation, as well as pip for development mode. ```bash cd python python setup.py build python setup.py install # Or using pip in development mode pip install -e . ``` -------------------------------- ### HTML Template Example Source: https://github.com/cozis/tinytemplate/blob/main/README.md An example of an HTML template file demonstrating TinyTemplate's syntax for variable substitution and control flow (for loops). This is a static example showing the expected input to the templating engine. ```html {{username}}s blog!

Hei, welcome to my blog! I'm {{name}} {{surname}} and I'm {{age}} years old!

``` -------------------------------- ### TinyTemplate Example params Callback (C) Source: https://github.com/cozis/tinytemplate/blob/main/README.md An example implementation of the `params` callback function. This specific example checks for a parameter named 'age' and sets its integer value to 24. Any other parameter key will result in `false` being returned. ```c bool params(void *data, const char *key, size_t len, tinytemplate_value_t *value) { if (len == 3 && !strncmp(key, "age", len)) { tinytemplate_set_int(value, 24); return true; } return false; } ``` -------------------------------- ### TinyTemplate Example stdout Callback (C) Source: https://github.com/cozis/tinytemplate/blob/main/README.md An example implementation of the `callback` function that redirects the evaluated template output to standard output (`stdout`). It uses `fwrite` to write the provided string segment to the console. ```c void callback(void *data, const char *str, size_t len) { fwrite(str, 1, len, stdout); } ``` -------------------------------- ### Command-Line Usage Example Source: https://github.com/cozis/tinytemplate/blob/main/README.md Demonstrates how to use the TinyTemplate command-line utility (`tt`). It shows piping a template file to stdin, providing parameters via a JSON file, and redirecting the output to an HTML file. This requires the `tt` executable to be built. ```sh cat page.tt | ./tt params.json > page.html ``` -------------------------------- ### CLI Basic Usage Source: https://context7.com/cozis/tinytemplate/llms.txt Shows fundamental command-line usage of the TinyTemplate processor ('tt'). Covers processing from stdin, using JSON files for parameters, and redirecting output. Includes an example JSON parameter file. ```bash # Simple template without parameters echo "Hello, World!" | ./tt > output.html # Template with JSON parameters cat page.tt | ./tt params.json > page.html # Example params.json: # { # "username": "john", # "name": "John", # "surname": "Doe", # "age": 30, # "posts": [ # {"title": "First Post", "date": "2024-01-01"}, # {"title": "Second Post", "date": "2024-01-15"} # ] # } # Inline template processing echo "Hello, {{name}}!" | ./tt <(echo '{"name":"Alice"}') > greeting.txt ``` -------------------------------- ### Conditional Rendering Example Source: https://context7.com/cozis/tinytemplate/llms.txt Shows how to conditionally render parts of a template based on the truthiness of a variable. Uses `{% if condition %} ... {% else %} ... {% end %}` syntax. ```plaintext // Template: "{% if logged_in %}Welcome back!{% else %}Please login.{% end %}" // With parameter logged_in=1 // Output: "Welcome back!" // With parameter logged_in=0 // Output: "Please login." ``` -------------------------------- ### Arithmetic Expressions Example Source: https://context7.com/cozis/tinytemplate/llms.txt Explains how to perform basic arithmetic operations like addition, multiplication, and modulo directly within template expressions using standard operators and the `mod` keyword. ```plaintext // Template: "Total: {{price * quantity}}" // With parameters price=10, quantity=3 // Output: "Total: 30" // Template: "{{a + b * c}}" // With parameters a=2, b=3, c=4 // Output: "14" // Template: "Remainder: {{value mod 10}}" // With parameter value=47 // Output: "Remainder: 7" ``` -------------------------------- ### Array Iteration Example Source: https://context7.com/cozis/tinytemplate/llms.txt Illustrates iterating over an array within a template using the `{% for item in items %}` construct. It also shows how to access the index with `{% for item, idx in items %}`. ```plaintext // Template: "" // With array items=["First", "Second", "Third"] // Output: "" // Template with index: "{% for item, idx in items %}{{idx}}: {{item}}\n{% end %}" // Output: "0: First\n1: Second\n2: Third\n" ``` -------------------------------- ### Variable Interpolation Example Source: https://context7.com/cozis/tinytemplate/llms.txt Demonstrates how to insert variable values into a template string using double curly braces `{{ variable }}`. This is a fundamental feature for dynamic content generation. ```plaintext // Template: "User: {{username}}, Email: {{email}}" // With parameters username="alice", email="alice@example.com" // Output: "User: alice, Email: alice@example.com" ``` -------------------------------- ### Object Property Access Example Source: https://context7.com/cozis/tinytemplate/llms.txt Demonstrates accessing properties of a dictionary (object) within a template using dot notation, e.g., `{{ object.property }}`. This allows for easy access to structured data fields. ```plaintext // Template: "{{user.name}} ({{user.age}} years old)" // With dict user having properties name="Bob" and age=25 // Output: "Bob (25 years old)" ``` -------------------------------- ### Building the CLI Tool Source: https://context7.com/cozis/tinytemplate/llms.txt Provides commands for compiling the TinyTemplate command-line interface (CLI) tool, 'tt'. Includes instructions for a standard build, cleaning artifacts, and the direct GCC compilation command. ```bash # Build the 'tt' executable make # Clean build artifacts make clean # Manual build command gcc src/cli.c src/tinytemplate.c 3p/xjson.c -o tt -Wall -Wextra -g -I3p ``` -------------------------------- ### Evaluate Compiled TinyTemplate Program with Callbacks (C) Source: https://context7.com/cozis/tinytemplate/llms.txt Executes a pre-compiled TinyTemplate bytecode program. It takes the original template, the bytecode, user data, a parameter callback function, an output callback function, and an error message buffer. The callbacks handle data retrieval and output writing, returning TINYTEMPLATE_STATUS_DONE on successful execution. ```c #include "tinytemplate.h" #include #include #include typedef struct { const char *name; int age; } user_data_t; bool params_callback(void *data, const char *key, size_t len, tinytemplate_value_t *value) { user_data_t *user = (user_data_t *)data; if (len == 4 && !strncmp(key, "name", len)) { tinytemplate_set_string(value, user->name, strlen(user->name)); return true; } if (len == 3 && !strncmp(key, "age", len)) { tinytemplate_set_int(value, user->age); return true; } return false; } void output_callback(void *data, const char *str, size_t len) { fwrite(str, 1, len, stdout); } int main(void) { char errmsg[128]; tinytemplate_instr_t program[64]; size_t num_instr; const char template[] = "Hello, {{name}}! You are {{age}} years old."; if (tinytemplate_compile(template, strlen(template), program, 64, &num_instr, errmsg, sizeof(errmsg)) != TINYTEMPLATE_STATUS_DONE) { fprintf(stderr, "Error: %s\n", errmsg); return -1; } user_data_t user = {.name = "Alice", .age = 30}; if (tinytemplate_eval(template, program, &user, params_callback, output_callback, errmsg, sizeof(errmsg)) != TINYTEMPLATE_STATUS_DONE) { fprintf(stderr, "Error: %s\n", errmsg); return -1; } return 0; } ``` -------------------------------- ### Library Usage: Compilation Source: https://github.com/cozis/tinytemplate/blob/main/README.md Compiles a template string into a bytecode program. This is the first step before evaluating the template. ```APIDOC ## Library Usage: Compilation Compiles a template string into a bytecode program. ### Function Signature ```c tinytemplate_status_t tinytemplate_compile(const char *src, size_t len, tinytemplate_instr_t *program, size_t max_instr, size_t *num_instr, char *errmsg, size_t errmax); ``` ### Parameters * **src** (const char *) - Pointer to the template source string. * **len** (size_t) - Length of the template source string in bytes. * **program** (tinytemplate_instr_t *) - Buffer to store the compiled bytecode instructions. * **max_instr** (size_t) - Maximum number of instructions the `program` buffer can hold. * **num_instr** (size_t *) - Pointer to a variable where the number of generated instructions will be stored upon success. * **errmsg** (char *) - Buffer to store error messages. * **errmax** (size_t) - Maximum size of the `errmsg` buffer. ### Return Value * **tinytemplate_status_t** - Status code indicating success (`TINYTEMPLATE_STATUS_DONE`) or failure. ### Error Handling * `TINYTEMPLATE_STATUS_EMEMORY`: If the `program` buffer is too small. * Other status codes indicate specific compilation errors. ### Example ```c #define COUNT(X) ((int) (sizeof(X) / sizeof((X)[0]))) int main(void) { char message[128]; tinytemplate_instr_t prog[32]; tinytemplate_status_t status; static const char text[] = "Hello, my name is {{name}}!"; size_t num_instr; status = tinytemplate_compile(text, strlen(text), prog, COUNT(prog), &num_instr, message, sizeof(message)); if (status != TINYTEMPLATE_STATUS_DONE) { fprintf(stderr, "Error: %s\n", message); return -1; } fprintf(stdout, "Program compiled to %ld instructions!\n", num_instr); return 0; } ``` ``` -------------------------------- ### Command-line Usage Source: https://github.com/cozis/tinytemplate/blob/main/README.md The `tt` utility processes template strings from stdin and outputs the evaluated version to stdout. Parameters can be provided via a JSON file. ```APIDOC ## Command-line Usage The `tt` utility accepts a template from stdin and writes the evaluated output to stdout. Errors are reported to stderr. Parameters can be supplied using a JSON file. ### Example ```sh cat page.tt | ./tt params.json > page.html ``` ``` -------------------------------- ### Compile TinyTemplate Template String to Bytecode (C) Source: https://context7.com/cozis/tinytemplate/llms.txt Compiles a template string into bytecode instructions using tinytemplate_compile. It requires the template string, its length, a buffer for bytecode, the buffer size, and output parameters for the number of instructions and error messages. It returns TINYTEMPLATE_STATUS_DONE on success. ```c #include "tinytemplate.h" #include #include #define COUNT(X) ((int) (sizeof(X) / sizeof((X)[0]))) int main(void) { char errmsg[128]; tinytemplate_instr_t program[64]; size_t num_instr; const char template[] = "Hello, {{name}}! You are {{age}} years old."; tinytemplate_status_t status = tinytemplate_compile( template, strlen(template), program, COUNT(program), &num_instr, errmsg, sizeof(errmsg) ); if (status != TINYTEMPLATE_STATUS_DONE) { fprintf(stderr, "Compilation error: %s\n", errmsg); return -1; } printf("Compiled to %zu instructions\n", num_instr); return 0; } ``` -------------------------------- ### Library Usage: Evaluation Source: https://github.com/cozis/tinytemplate/blob/main/README.md Evaluates a compiled bytecode program to generate the final output. Requires parameters to be provided. ```APIDOC ## Library Usage: Evaluation Evaluates a compiled template program to produce the final output string. ### Function Signature ```c tinytemplate_status_t tinytemplate_eval(const char *src, const tinytemplate_instr_t *program, void *userp, tinytemplate_getter_t params, tinytemplate_callback_t callback, char *errmsg, size_t errmax); ``` ### Parameters * **src** (const char *) - The original template source string (used for error reporting). * **program** (const tinytemplate_instr_t *) - Pointer to the compiled bytecode instructions. * **userp** (void *) - User-defined pointer, passed to `params` and `callback`. * **params** (tinytemplate_getter_t) - A function pointer (or callback) used to retrieve template variable values. Its signature is typically `value_t params(void *userp, const char *key)`. * **callback** (tinytemplate_callback_t) - A function pointer (or callback) used to append generated output. Its signature is typically `void callback(void *userp, const char *s, size_t len)`. * **errmsg** (char *) - Buffer to store error messages during evaluation. * **errmax** (size_t) - Maximum size of the `errmsg` buffer. ### Return Value * **tinytemplate_status_t** - Status code indicating success (`TINYTEMPLATE_STATUS_DONE`) or failure. ### Error Handling * Errors during evaluation (e.g., missing variables, invalid constructs) are reported via `errmsg`. ``` -------------------------------- ### C Library: Compile Template Source: https://github.com/cozis/tinytemplate/blob/main/README.md This C code snippet shows how to compile a template string into bytecode using the `tinytemplate_compile` function. It handles potential compilation errors by checking the return status and printing error messages. Dependencies include `tinytemplate.h`, `tinytemplate.c`, and standard C libraries. ```c #define COUNT(X) ((int) (sizeof(X) / sizeof((X)[0]))) int main(void) { char message[128]; tinytemplate_instr_t prog[32]; tinytemplate_status_t status; static const char text[] = "Hello, my name is {{name}}!"; size_t num_instr; status = tinytemplate_compile(text, strlen(text), prog, COUNT(prog), NULL, message, sizeof(message)); if (status != TINYTEMPLATE_STATUS_DONE) { fprintf(stderr, "Error: %s", message); return -1; } fprintf(stdout, "Program compiled to %ld instructions!\n", num_instr); return 0; } ``` -------------------------------- ### C Library: Compile Function Signature Source: https://github.com/cozis/tinytemplate/blob/main/README.md This C code snippet shows the function signature for `tinytemplate_compile`, used to convert a template source string into an instruction program. It takes the source string, its length, a buffer for instructions, maximum instruction capacity, and buffers for output and error messages. ```c tinytemplate_status_t tinytemplate_compile(const char *src, size_t len, tinytemplate_instr_t *program, size_t max_instr, size_t *num_instr, char *errmsg, size_t errmax); ``` -------------------------------- ### C Library: Evaluate Template Source: https://github.com/cozis/tinytemplate/blob/main/README.md This C code snippet outlines the function signature for `tinytemplate_eval`, which evaluates a compiled template program. It requires the compiled program, user data, a parameter getter function, and a callback function for output. Error messages are provided via a buffer. ```c tinytemplate_status_t tinytemplate_eval(const char *src, const tinytemplate_instr_t *program, void *userp, tinytemplate_getter_t params, tinytemplate_callback_t callback, char *errmsg, size_t errmax); ``` -------------------------------- ### TinyTemplate params Callback Interface (C) Source: https://github.com/cozis/tinytemplate/blob/main/README.md The `params` callback function interface for retrieving parameter values during template evaluation. It takes user data, the parameter key and its length, and a pointer to a `tinytemplate_value_t` to store the result. It should return `true` if the parameter is found and set, `false` otherwise. ```c bool params(void *data, const char *key, size_t len, tinytemplate_value_t *value); ``` -------------------------------- ### Setting Dictionary Values in C Source: https://context7.com/cozis/tinytemplate/llms.txt Configures a C struct to be accessed as a dictionary within templates using dot notation. A getter callback function is required to retrieve property values based on their keys. This enables structured data access in templates. ```c typedef struct { const char *title; const char *author; int year; } book_t; bool book_getter(void *data, const char *key, size_t len, tinytemplate_value_t *value) { book_t *book = (book_t *)data; if (len == 5 && !strncmp(key, "title", len)) { tinytemplate_set_string(value, book->title, strlen(book->title)); return true; } if (len == 6 && !strncmp(key, "author", len)) { tinytemplate_set_string(value, book->author, strlen(book->author)); return true; } if (len == 4 && !strncmp(key, "year", len)) { tinytemplate_set_int(value, book->year); return true; } return false; } bool params_callback(void *data, const char *key, size_t len, tinytemplate_value_t *value) { static book_t book = { .title = "1984", .author = "George Orwell", .year = 1949 }; if (len == 4 && !strncmp(key, "book", len)) { tinytemplate_set_dict(value, &book, book_getter); return true; } return false; } ``` -------------------------------- ### Set Float Values for TinyTemplate Variables (C) Source: https://context7.com/cozis/tinytemplate/llms.txt Illustrates setting floating-point values in the `params_callback` for TinyTemplate. This code snippet checks for keys like 'price' or 'pi' and uses `tinytemplate_set_float` to assign double-precision floating-point values. ```c bool params_callback(void *data, const char *key, size_t len, tinytemplate_value_t *value) { if (len == 5 && !strncmp(key, "price", len)) { tinytemplate_set_float(value, 19.99); return true; } if (len == 2 && !strncmp(key, "pi", len)) { tinytemplate_set_float(value, 3.14159); return true; } return false; } ``` -------------------------------- ### Setting Array Values in C Source: https://context7.com/cozis/tinytemplate/llms.txt Configures a C array to be iterated within templates using the 'for' loop construct. It requires a callback function to manage the array state and provide values to the template engine. Dependencies include standard C libraries and the tinytemplate library. ```c typedef struct { const char **items; size_t count; size_t current; } array_state_t; bool array_next(void *data, tinytemplate_value_t *value) { array_state_t *state = (array_state_t *)data; if (state->current < state->count) { const char *item = state->items[state->current++]; tinytemplate_set_string(value, item, strlen(item)); return true; } return false; } bool params_callback(void *data, const char *key, size_t len, tinytemplate_value_t *value) { static const char *fruits[] = {"Apple", "Banana", "Orange"}; static array_state_t state; if (len == 6 && !strncmp(key, "fruits", len)) { state.items = fruits; state.count = 3; state.current = 0; tinytemplate_set_array(value, &state, array_next); return true; } return false; } ``` -------------------------------- ### Set String Values for TinyTemplate Variables (C) Source: https://context7.com/cozis/tinytemplate/llms.txt Shows how to set string values for TinyTemplate variables within the `params_callback`. It uses `tinytemplate_set_string`, providing the string pointer and its length, which is crucial for handling C strings correctly. ```c bool params_callback(void *data, const char *key, size_t len, tinytemplate_value_t *value) { if (len == 5 && !strncmp(key, "title", len)) { const char *str = "My Blog Post"; tinytemplate_set_string(value, str, strlen(str)); return true; } if (len == 7 && !strncmp(key, "content", len)) { const char *str = "This is the content..."; tinytemplate_set_string(value, str, strlen(str)); return true; } return false; } ``` -------------------------------- ### Set Integer Values for TinyTemplate Variables (C) Source: https://context7.com/cozis/tinytemplate/llms.txt Demonstrates setting integer values within the `params_callback` function for TinyTemplate. It checks the key length and name to assign integer values like 42 or -1 to the `tinytemplate_value_t` structure using `tinytemplate_set_int`. ```c bool params_callback(void *data, const char *key, size_t len, tinytemplate_value_t *value) { if (len == 5 && !strncmp(key, "count", len)) { tinytemplate_set_int(value, 42); return true; } if (len == 6 && !strncmp(key, "status", len)) { tinytemplate_set_int(value, -1); return true; } return false; } ``` -------------------------------- ### TinyTemplate callback Function Interface (C) Source: https://github.com/cozis/tinytemplate/blob/main/README.md The `callback` function interface used by TinyTemplate to output evaluated string segments. It receives user data and a pointer to the string data and its length. This function is responsible for handling the output, typically by writing to a buffer or stream. ```c void callback(void *data, const char *str, size_t len); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.