Templates
Fill a text template with JSON data, with loops, conditions, exact calculations and formats, from a string or a file. Typical uses: produce mails, reports, XML or EDI messages from data.
template rendertemplate rendertemplate render_filetemplate render_file

template render <template> <data>


Description

    Fill a template. Returns the text

Parameters

    template:   The template - String - required
    data:   The data, a JSON object or array - String - required
admin
template render "Hello {{name}}!" "{\"name\": \"Alice\"}"
mentdb
Hello Alice!
a loop with a calculation in exact decimals, a sum and French number format
admin
template render "{{#each items as it}} {{loop.index}}. {{it.sku}}: {{it.qty * it.price | number:2,',',' '}} EUR {{/each}} Articles: {{items | sum:'qty'}}" "{\"customer\": {\"name\": \"<Bob> & Co\", \"vip\": true}, \"items\": [{\"sku\": \"A1\", \"qty\": 2, \"price\": \"12.50\"}, {\"sku\": \"B2\", \"qty\": 1, \"price\": 9.99}], \"date\": \"2026-03-29\"}"
mentdb
1. A1: 25,00 EUR 2. B2: 9,99 EUR Articles: 3
conditions
admin
template render "{{#if customer.vip and items | length > 1}}VIP order{{#elseif customer.vip}}VIP{{else}}Standard{{/if}}" "{\"customer\": {\"name\": \"<Bob> & Co\", \"vip\": true}, \"items\": [{\"sku\": \"A1\", \"qty\": 2, \"price\": \"12.50\"}, {\"sku\": \"B2\", \"qty\": 1, \"price\": 9.99}], \"date\": \"2026-03-29\"}"
mentdb
VIP order
a running total, and a date in French
admin
template render "{{#set total = 0}}{{#each items as it}}{{#set total = total + it.qty * it.price}}{{/each}}{{date | date:'d MMMM yyyy','fr'}}: {{total}}" "{\"customer\": {\"name\": \"<Bob> & Co\", \"vip\": true}, \"items\": [{\"sku\": \"A1\", \"qty\": 2, \"price\": \"12.50\"}, {\"sku\": \"B2\", \"qty\": 1, \"price\": 9.99}], \"date\": \"2026-03-29\"}"
mentdb
29 mars 2026: 34.99
nothing to list, a default value
admin
template render "{{#each missing as m}}x{{else}}no line{{/each}}, {{phone | default:'no phone'}}" "{\"customer\": {\"name\": \"<Bob> & Co\", \"vip\": true}, \"items\": [{\"sku\": \"A1\", \"qty\": 2, \"price\": \"12.50\"}, {\"sku\": \"B2\", \"qty\": 1, \"price\": 9.99}], \"date\": \"2026-03-29\"}"
mentdb
no line, no phone

template render <template> <data> <params>


Description

    Fill a template with parameters: escape (none, or html: every value is escaped, unless it ends with | raw or is written {{{x}}}), strict (false; true: an unknown variable is an error), max_output (10 MB, up to 64 MB, in characters), max_steps (20000000, up to 1000000000), timeout_ms (5000, up to 60000)

Parameters

    template:   The template - String - required
    data:   The data, a JSON object or array - String - required
    params:   The parameters as JSON - String - required
admin
template render "<p>{{customer.name}}</p>" "{\"customer\": {\"name\": \"<Bob> & Co\", \"vip\": true}, \"items\": [{\"sku\": \"A1\", \"qty\": 2, \"price\": \"12.50\"}, {\"sku\": \"B2\", \"qty\": 1, \"price\": 9.99}], \"date\": \"2026-03-29\"}" "{\"escape\": \"html\"}"
mentdb
<p><Bob> & Co</p>
a value that holds HTML on purpose
admin
template render "<p>{{{note}}}</p>" "{\"note\": \"<b>ok</b>\"}" "{\"escape\": \"html\"}"
mentdb
<p><b>ok</b></p>
a method call is refused
admin
template render "{{customer.getClass()}}" "{\"customer\": {\"name\": \"<Bob> & Co\", \"vip\": true}, \"items\": [{\"sku\": \"A1\", \"qty\": 2, \"price\": \"12.50\"}, {\"sku\": \"B2\", \"qty\": 1, \"price\": 9.99}], \"date\": \"2026-03-29\"}"
mentdb
ERROR: Sorry, an error appears (template line 1: a template cannot call a method or a function in 'customer.getClass()').
a missing variable as an error
admin
template render "{{nope}}" "{}" "{\"strict\": true}"
mentdb
ERROR: Sorry, an error appears (template line 1: unknown variable 'nope').
Used in use cases (1 recipe)

    Integrator & ETL developer › Write an order confirmation from a template, with the customer data escaped
Often used with

    string char

template render_file <file> <data>


Description

    Fill a template read from a file (UTF-8, 4 MB at most), through the usual rights on files. Same syntax. Parameters as template render

Parameters

    file:   The path of the template - String - required
    data:   The data, a JSON object or array - String - required
admin
template render_file "tmp/hello.tpl" "{\"name\": \"Alice\"}"
mentdb
Hello Alice!

template render_file <file> <data> <params>


Description

    Fill a template read from a file, with the parameters of template render

Parameters

    file:   The path of the template - String - required
    data:   The data, a JSON object or array - String - required
    params:   The parameters as JSON - String - required
admin
template render_file "tmp/hello.tpl" "{\"name\": \"<Alice>\"}" "{\"escape\": \"html\"}"
mentdb
Hello <Alice>!