Skip to main content

Miscellaneous

#log​

Logs values to the tracer. It does not render output.

{{log "Rendering product" name}}

Block form:

{{#log level="debug"}}
Product: {{name}},
Price: {{price}}
{{/log}}

Log levels are NOT supported.

either​

Returns the first non-falsy value among context and params

Usage:

{{either preferredLabel fallbackLabel "N/A"}}

If the preferredLabel is true value, it will be return.

If the preferredLabel is falsy value, it will return fallbackLabel.

If the fallbackLabelis falsy, it will return "N/A"

filter​

Filter the context object by one or more pairs of field and field values and return an array of the resulting item. Optionally, matching can be done using XPath, which allows for more complex conditions.

{{filter . 'name' 'Sue' }} → filter for all elements where the name attribute is 'Sue'
{{filter . 'name' 'Sue' 'children' 2 }} → filter for all elements where the name attribute is 'Sue' OR the children attribute equals the number '2'.
{{filter . 'not(names/firstname = \'Sue\')' xpath=true }} → filter for all elements where the firstname attribute of the name object does NOT equal 'Sue'
0: field name: (string) name of a JSON property
1: value: (variant) to match against the content of the field
2, 3, etc.: further fieldName-value pairs matching in an OR matter

generateId​

Create a document local unique ID for a data context.

Usage:

{{generateId this }} → 'i41ccb3b91b0b' (a locally unique ID)

Typically, used for creating unique IDs for text anchors.

See the similar XPath 1.0 function.

sort​

<ul>
{{#each (sort people 'firstname' order='ascending')}}
<li>{{firstname}} {{lastname}}</li>
{{/each}}
</ul>

For data context:

{"people": [
{"firstname": "Susan", "lastname": "Doe", "age": 21, "country": "AT"},
{"firstname": "John", "lastname": "Doe", "age": 23, "country": "DE"}
]}

Result:

<ul>
<li>John Doe</li>
<li>Susan Doe</li>
</ul>

Sort can be embedded in other sort.

when​

Ternary helper with optional regex guard when context is truthy

Usage:

{{when active "Enabled" "Disabled"}}

If apply below input:

{
"active": true
}

The result will be Enabled. If "active": false, the result will be Disabled

This helper comes with regex option:

Example:

{{when
email
"VALID EMAIL"
"INVALID EMAIL"
regex="^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$"
}}

If the input data is:

{
"email": "john@example.com"
}

Result will be VALID EMAIL If "email": "abc", result will be INVALID EMAIL.

xpath​

This xpath helper evaluates a XPath expression against the current context. This allows using complex XPath expressions in the template, e.g., to 'look up' or 'look aside' in the data. The JXPath engine is used for evaluation, both for XML and JSON (https://commons.apache.org/proper/commons-jxpath/).

Usage:

With this input data:

{
"product": {
"name": "Laptop",
"price": 1200,
"category": {
"name": "Electronics"
},
"variants": [
"a",
"b",
"c"
]
}
}

Template:


<div>
Name: {{xpath product "name"}}</p>
Catogory: {{xpath product "category/name"}}</p>
Number of Variants: {{xpath . "count(product/variants)"}}</p>
</div>

Result:


<div>
Name: Laptop</p>
Category: Electronics</p>
Number of Variants: 3</p>
</div>