Spanish Total.js
English
Documentation For AI agents
Contents

Tangular helpers

The helpers available in every Tangular template for encoding, formatting and reading code lists.

.md GitHub
On this page

A helper follows the value in a template, as in {{ price | format(2) }}, and helpers can be chained. A value with no helper is encoded with encode. To write templates and register custom helpers, see Templates with Tangular.

API

method

{{ id | cl(name, [key]) }}

Writes a field of an item in a code list.
ParameterTypeDescription
name String The code list name.
key? String The field to write. Default: name

Returns String The value of the field.

The code list is the array in DEF.cl[name], and the item is found by its id. When nothing is found, the helper writes DEF.empty, or an empty string when key is set.

Reading a code list
DEF.cl.colors = [{ id: 2, name: 'Red', code: 'R' }];
console.log(Tangular.render("{{ id | cl('colors') }}", { id: 2 })); // Red
console.log(Tangular.render("{{ id | cl('colors', 'code') }}", { id: 2 })); // R
console.log(Tangular.render("{{ id | cl('colors') }}", { id: 9 })); // ---
method

{{ id | clcolor(name, [key]) }}

Writes an item of a code list as a colored label.
ParameterTypeDescription
name String The code list name.
key? String The field to write. Default: name

Returns String The HTML of the label.

The helper writes a <span class="ui-clcolor"> with the item color as its background, the item icon and the field. It writes an empty string when the item is not found. The output is not encoded.

A colored label
DEF.cl.colors = [{ id: 2, name: 'Red', icon: 'ti ti-circle', color: '#f00' }];
console.log(Tangular.render("{{ id | clcolor('colors') }}", { id: 2 })); // <span class="ui-clcolor" style="background:#f00"><i class="ti ti-circle mr5"></i>Red</span>
method

{{ id | clicon(name, [key]) }}

Writes an item of a code list with its icon.
ParameterTypeDescription
name String The code list name.
key? String The field to write. Default: name

Returns String The HTML of the item.

The icon takes the item color. An item without an icon writes only the field, and an item that is not found writes an empty string. The output is not encoded.

An item with an icon
DEF.cl.colors = [{ id: 1, name: 'Plain' }, { id: 2, name: 'Red', icon: 'ti ti-circle', color: '#f00' }];
console.log(Tangular.render("{{ id | clicon('colors') }}", { id: 2 })); // <i class="ti ti-circle mr5" style="color:#f00"></i>Red
console.log(Tangular.render("{{ id | clicon('colors') }}", { id: 1 })); // Plain
method

{{ value | currency([name]) }}

Formats a number or a numeric string with a registered currency.
ParameterTypeDescription
name? String The currency name. Default: DEF.currency

Returns String The formatted price.

The helper uses currency(). A value of another type writes an empty string.

Prices
DEF.currencies.eur = value => value.format(2) + ' EUR';
console.log(Tangular.render("{{ price | currency('eur') }}", { price: 5 })); // 5.00 EUR
console.log(Tangular.render("{{ price | currency('eur') }}", { price: '7.5' })); // 7.50 EUR
method

{{ value | def([text], [raw]) }}

An alias of the empty helper.
ParameterTypeDescription
text? String The text for an empty value. Default: DEF.empty
raw? Boolean true writes the value without encoding. Default: false

Returns String The value or the text.

Default text
console.log(Tangular.render("{{ name | def('n/a') }}", { name: null })); // n/a
method

{{ value | empty([text], [raw]) }}

Writes a default text for an empty value.
ParameterTypeDescription
text? String The text for an empty value. Default: DEF.empty
raw? Boolean true writes the value without encoding. Default: false

Returns String The value or the text.

Every falsy value is empty, including 0. Other values are encoded unless raw is true. Passing true as the first argument also turns on raw.

Empty values
console.log(Tangular.render('{{ name | empty }}', { name: '' })); // ---
console.log(Tangular.render("{{ name | empty('n/a') }}", { name: '' })); // n/a
console.log(Tangular.render('{{ name | empty }}', { name: '<b>' })); // &lt;b&gt;
console.log(Tangular.render('{{ name | empty(true) }}', { name: '<b>' })); // <b>
method

{{ value | encode([type]) }}

Encodes a value for output.
ParameterTypeDescription
type? String json for JSON, json2 for indented JSON, url or urlencoded for encodeURIComponent(), and querify for a query string.

Returns String The encoded value.

Without type, the helper escapes <, >, & and ", and writes an empty string for null and undefined.

Encoding
console.log(Tangular.render('{{ value | encode }}', { value: '<b>' })); // &lt;b&gt;
console.log(Tangular.render("{{ value | encode('json') }}", { value: { a: 1 } })); // {"a":1}
console.log(Tangular.render("{{ value | encode('url') }}", { value: 'a b' })); // a%20b
console.log(Tangular.render("{{ value | encode('querify') }}", { value: { a: 1, b: 'x y' } })); // ?a=1&b=x%20y
method

{{ value | format([format], [separator], [decimalSeparator]) }}

Formats a number or a date.
ParameterTypeDescription
format? Number or String The decimal places for a number, or the layout for a date.
separator? String The thousands separator of a number.
decimalSeparator? String The decimal separator of a number.

Returns String The formatted value.

For a number, the arguments are those of format(), and a number without decimals is written without its decimal part. For a Date, the first argument is the layout of format().

A string that holds a date is converted first and uses dd.MM.yyyy by default, and a string that holds a number is converted to a number. null and undefined write an empty string.

Numbers
console.log(Tangular.render('{{ value | format(2) }}', { value: 1234.5 })); // 1 234.50
console.log(Tangular.render("{{ value | format(2, ',', '.') }}", { value: 1234.5 })); // 1,234.50
console.log(Tangular.render('{{ value | format }}', { value: 1234.5 })); // 1 234
Dates
var date = new Date(2026, 10, 8, 15, 4);
console.log(Tangular.render("{{ value | format('dd.MM.yyyy HH:mm') }}", { value: date })); // 08.11.2026 15:04
console.log(Tangular.render('{{ value | format }}', { value: '2026-11-08' })); // 08.11.2026
method

{{ value | pluralize(zero, one, few, other) }}

Selects the text that matches a number.
ParameterTypeDescription
zero String The text for 0.
one String The text for 1.
few String The text for numbers from 2 to 4.
other String The text for other numbers.

Returns String The selected text.

The arguments are those of pluralize(). A numeric string is converted to a number, and an empty value counts as 0.

Counting items
var template = "{{ count | pluralize('no items', '# item', '# items', '# items') }}";
console.log(Tangular.render(template, { count: 3 })); // 3 items
console.log(Tangular.render(template, { count: 1 })); // 1 item
console.log(Tangular.render(template, { count: null })); // no items
method

{{ value | raw }}

Writes a value without encoding.

Returns Object The unchanged value.

Use it only for content that is safe to insert as HTML.

Writing HTML
console.log(Tangular.render('{{ value | raw }}', { value: '<b>Hi</b>' })); // <b>Hi</b>
console.log(Tangular.render('{{ value }}', { value: '<b>Hi</b>' })); // &lt;b&gt;Hi&lt;/b&gt;
Made in Europe