Insert Values
The most fundamental pattern is inserting a dynamic value directly into your prompt. Use the{{.key}} syntax to reference a specific field from your JSON payload.
Input
Template
Result
<no value> instead of raising an error or failing the execution.
Conditional Logic
Not all instructions apply to every scenario. You can conditionally include or exclude sections of your prompt by usingif, else, and end blocks. This ensures your model only receives instructions relevant to the current context, saving context window tokens and reducing model confusion.
Input
Template
Result
nil, and zero values as false, allowing you to safely handle optional user inputs.
Logical and Comparison Operators
You can build complex conditions using logical functions (and, or, not) and comparison operators (eq, ne, lt, le, gt, ge). These functions use a prefix syntax where the operator precedes the arguments.
Input
Template
Result
Iterate Over Lists
When your input data contains an array of items, use therange block to repeat a prompt segment for each element. This is useful for passing batch tasks, search results, or user options directly into the prompt.
Input
Template
Result
range block, the dot notation {{.}} refers directly to the current item being processed.
Iterate Over Structured Lists
Lists often contain structured objects rather than simple text values. Within arange loop, you can access the fields of each individual object directly using dot notation.
Input
Template
Result
Access Root and Parent Scope inside Loops
Within arange block, the context . is rebound to the active element. To reference variables from the root of the JSON payload inside a loop, prefix the path with $. to escape the local scope.
Input
Template
Result
Access Nested Data and Context Rebinding
JSON payloads frequently contain hierarchical, nested objects. You can navigate these deeper data structures directly within your prompt template by chaining keys with dot notation.Input
Template
Result
Context Rebinding with with
When navigating deeply nested objects, chaining can become repetitive. The with block binds the context . to the specified object within its scope. If the target object is empty, optional, or absent, the with block can execute an optional else block.
Input
Template
Result
Variables and Assignment
You can declare custom variables inside templates to store values for reuse or to simplify access inside nested scopes. Declare and assign a variable using the{{$name := value}} syntax.
Input
Template
Result
Index and Key Capture in Loops
When iterating over a list usingrange, you can capture the index of the element alongside its value.
Input
Template
Result
Comments
To document your template without passing instructions or meta-commentary to the language model, use template comments. Comments are completely omitted during the rendering process, saving model context tokens.Input
Template
Result
Whitespace Control
In prompt engineering, extra newlines and spaces can consume unnecessary tokens or affect the structural formatting of model instructions. By default, template tags leave surrounding spaces and newlines intact. You can trim adjacent whitespace and newlines by adding a hyphen- to the template tags.
{{-trims whitespace and newlines immediately preceding the tag.-}}trims whitespace and newlines immediately following the tag.
Input
Template
Result
{{- and -}}, the conditional logic blocks are cleanly evaluated without introducing unwanted empty lines or spaces in the final prompt.
Built-in Utility Functions
The template engine includes several built-in functions to manipulate and format data during prompt rendering.Length Check (len)
Use the len function to retrieve the number of elements in a list, map, or the number of characters in a string.
Input
Template
Result
Direct Indexing (index)
Use the index function to retrieve a specific element from an array or map by passing the array or map followed by the index keys.
Input
Template
Result
String Formatting (printf)
Use the printf function to construct formatted strings using verbs (such as %s for strings, %d for integers, or %.2f for float precision).
Input
Template
Result
Provide Default Values
To protect your prompts from incomplete or missing input fields, you can define fallback values using thedefault function. This prevents rendering empty spaces or generic text when a user or upstream system provides sparse data.

