Template Text
Build text using a text template - for generating text in formats other than HTML, where you donβt need automatic escaping and you have full control over the format of the output.
Basic Example
A sample template that outputs a greeting:
Matching Data:
This will output:
Note: numbers in templating are always treated as decimals. ao if you want to compare numbers use decimals like 1.0
instead of 1
.
Field names in templates canβt have spaces or special characters.
Accessing Nested Data
For nested data, dot notation is used to access fields in objects and to access items in arrays you need to use the index
function.
Example:
Then, in your template:
Array and String (Text) Slicing
Matching JSON Data:
Explanation:
-
Array slicing:
- slice .numbers 1 3 returns [20, 30] (elements from index 1 to 3, exclusive).
- slice .numbers 0 2 returns [10, 20] (elements from the start to index 2, exclusive).
- slice .numbers 2 returns [30, 40, 50] (elements from index 2 to the end).
- slice .numbers 1 4 4 is equivalent to Go syntax numbers[1:4:4] (advanced slicing for specifying capacity, often used for reslicing).
-
String slicing:
- slice .text 1 4 returns βellβ (characters from index 1 to 4, exclusive).
Logic Examples
Using a Boolean Field:
Matching Data:
If-Else Example:
Matching Data:
Using with
With
sets the scope to a specific data structure, reducing complexity when accessing nested fields.
Example:
Matching Data:
Explanation: Inside the with block, . refers to .user. This reduces complexity when accessing nested fields.
Define a Sub Template
Define a Sub Template: Use define to create reusable named sub templates:
Invoke a Defined Template: Use template to invoke a named template:
Logical Operators
The template engine provides several logical operators for complex conditions:
Operator | Description | Example | Result |
---|---|---|---|
and | Returns true if both conditions are true | {{ if and .isAdmin .isActive }} | True only if both isAdmin and isActive are true |
or | Returns true if either condition is true | {{ if or .isAdmin .isModerator }} | True if either isAdmin or isModerator is true |
not | Inverts a boolean value | {{ if not .isDisabled }} | True if isDisabled is false |
Example usage:
Matching Data:
This will output:
Note: You can combine multiple operators and use parentheses to control precedence:
Comparison Functions
The template engine provides several comparison operators for numeric and text (string) values:
Operator | Description | Example | Result |
---|---|---|---|
eq | Equal to | {{ if eq .count 10.0 }} | True if count equals 10.0 |
ne | Not equal to | {{ if ne .status "error" }} | True if status is not βerrorβ |
lt | Less than | {{ if lt .price 20.0 }} | True if price is less than 20.0 |
le | Less than or equal to | {{ if le .quantity 5.0 }} | True if quantity is less than or equal to 5.0 |
gt | Greater than | {{ if gt .age 18.0 }} | True if age is greater than 18.0 |
ge | Greater than or equal to | {{ if ge .score 90.0 }} | True if score is greater than or equal to 90.0 |
Example usage:
Matching Data:
This will output:
Note: All number comparisons in Comnoco templating must use decimal numbers (e.g., use 10.0
instead of 10
).
Escaping Functions
Use specialized functions to escape output.
HTML Escaping:
JavaScript Escaping:
URL Query Escaping:
Adding Variables
You can declare and use variables within your template. Variables are local to the scope they are defined in. Examples:
Trim Markers
Use trim markers ({{-
and -}}
) to manage whitespace in your templates. This helps keep nested templates readable and clean, especially with conditional blocks.
Example:
Matching Data:
This will output (notice the lack of extra whitespace):
Without trim markers, the same template would output:
Important Notes:
{{-
(with space) trims all whitespace before the block-}}
(with space) trims all whitespace after the block- Be careful with
{{-3}}
vs{{- 3}}
- the first is treated as a negative number, the second trims whitespace before outputting 3 - Trim markers are especially useful in loops and conditional blocks to prevent unwanted line breaks and indentation
Comments
Use comments to document or temporarily disable parts of a template during development or debugging. Single-line comment:
Multi-line comment:
With Trim Markers:
Print Functions
The template engine provides several print functions for formatting output:
Basic Print Functions
Matching Data:
This will output:
Format Specifiers
Common format specifiers for use with printf
:
Matching Data:
This will output:
Note: Since all numbers in Comnoco are treated as decimals/floats, always use float formatting specifiers (%f
, %e
, %E
, %v
) rather than integer specifiers like %d
.
Returns |
---|
Text |
Parameters
Parameter | Parameter | Valid Blocks | Required |
---|---|---|---|
Set an Override Name | Show the override name slot in the tree so that it can be set statically or dynamically | Boolean Block Group | No |
Override Name | The override name of this item. Use this to replace the inherited name with a custom name. Expose it on the tree to set it dynamically. | Text Block Group | No |
Attributes | Metadata values for this block. Used in XML and multipart/form-data. | Text Block Group | No |
Template to use | The text template to use. Substitute values into the template using the dot notation, e.g. 'open curly brace' 'open curly brace' .keyname 'close curly brace' 'close curly brace' | Text Block Group | No |
Data | The values to substitute. Must be a Data Structure or valid JSON. Key names must be referenced in the template using the dot notation, e.g. 'open curly brace' 'open curly brace' .keyname 'close curly brace' 'close curly brace' | Marshalled Data Block Group | No |