Spanish Total.js
English
Documentation For AI agents
Contents

Tangular helpers

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

.md GitHub
On this page

A helper transforms a value in a template: {{ value | helper(arguments) }}. These helpers live in the global object Thelpers, so they can also be called from code with the value as the first argument. A value without a helper is HTML-encoded automatically; a value with a helper is written as the helper returns it. Templates with Tangular explains the syntax and how to add your own helpers.

API

method

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

Writes a value of the code list item with the given identifier.
ParameterTypeDescription
name String The name of the code list.
key? String The field to write. Default: 'name'

Returns String The value of the item.

The code list is read from DEF.cl[name], and its items are matched by the field id. Without key, the helper writes the field name, or DEF.empty when the item does not exist. With key, a missing item writes an empty text. The output is not HTML-encoded.

Read a code list
DEF.cl.colors = [
	{ id: 1, name: 'Red', icon: 'ti ti-circle', color: '#f00' },
	{ id: 2, name: 'Blue' }
];

'{{ id | cl("colors") }}'.render({ id: 1 }); // "Red"
'{{ id | cl("colors", "color") }}'.render({ id: 1 }); // "#f00"
'{{ id | cl("colors") }}'.render({ id: 9 }); // "---"
method

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

Writes a code list item as a colored <span>.
ParameterTypeDescription
name String The name of the code list.
key? String The field to write. Default: 'name'

Returns String The HTML of the item.

The element has the class ui-clcolor. The color field of the item sets its background and the icon field adds an icon. A missing item writes an empty text. The output is not HTML-encoded.

A colored label
DEF.cl.colors = [
	{ id: 1, name: 'Red', icon: 'ti ti-circle', color: '#f00' },
	{ id: 2, name: 'Blue' }
];

'{{ id | clcolor("colors") }}'.render({ id: 1 }); // "<span class=\"ui-clcolor\" style=\"background:#f00\"><i class=\"ti ti-circle mr5\"></i>Red</span>"
'{{ id | clcolor("colors") }}'.render({ id: 2 }); // "<span class=\"ui-clcolor\">Blue</span>"
method

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

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

Returns String The HTML of the item.

The icon field adds an <i> element, and the color field sets the color of the icon. A missing item writes an empty text. The output is not HTML-encoded.

An item with an icon
DEF.cl.colors = [
	{ id: 1, name: 'Red', icon: 'ti ti-circle', color: '#f00' },
	{ id: 2, name: 'Blue' }
];

'{{ id | clicon("colors") }}'.render({ id: 1 }); // "<i class=\"ti ti-circle mr5\" style=\"color:#f00\"></i>Red"
'{{ id | clicon("colors") }}'.render({ id: 2 }); // "Blue"
method

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

Formats a number or a numeric text as a currency.
ParameterTypeDescription
code? String The currency code registered in DEF.currencies.

Returns String The formatted amount.

The helper calls currency(). Values that are not numbers or texts write an empty text.

Write an amount
DEF.currencies.eur = value => value.format(2) + ' EUR';

'{{ price | currency("eur") }}'.render({ price: 1234.5 }); // "1 234.50 EUR"
'{{ price | currency("eur") }}'.render({ price: '1234,5' }); // "1 234.50 EUR"
'{{ price | currency("eur") }}'.render({ price: null }); // ""
method

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

Writes the value, or a replacement when the value is empty.
ParameterTypeDescription
text? String The replacement text. Default: DEF.empty

Returns String The encoded value or the replacement.

Every falsy value counts as empty, including 0. The value is HTML-encoded. The replacement defaults to DEF.empty, which is ---. empty is another name for this helper.

Replace an empty value
'{{ name | def }}'.render({ name: '' }); // "---"
'{{ name | def("unknown") }}'.render({}); // "unknown"
'{{ name | def }}'.render({ name: '<b>' }); // "&lt;b&gt;"
'{{ count | def }}'.render({ count: 0 }); // "---"
method

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

Another name for the helper def.
ParameterTypeDescription
text? String The replacement text. Default: DEF.empty

Returns String The encoded value or the replacement.

Replace an empty value
'{{ name | empty }}'.render({}); // "---"
'{{ name | empty("n/a") }}'.render({ name: 'Ann' }); // "Ann"
method

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

Encodes the value for HTML, JSON or a URL.
ParameterTypeDescription
type? String or Function The type of encoding.

Returns String The encoded value.

Without type, the characters <, >, & and " are written as HTML entities and null or undefined writes an empty text.

TypeOutput
'json'JSON
'json2'JSON indented with tabs
'url', 'urlencoded'A URL-encoded value
'querify'A query string built with QUERIFY()
FunctionThe result of function(value)
Encoding types
'{{ value | encode }}'.render({ value: '<b>' }); // "&lt;b&gt;"
'{{ value | encode("json") }}'.render({ value: { id: 1 } }); // "{\"id\":1}"
'{{ value | encode("url") }}'.render({ value: 'a b&c' }); // "a%20b%26c"
'{{ value | encode("querify") }}'.render({ value: { page: 1, q: 'x y' } }); // "?page=1&q=x%20y"
method

{{ value | format([format], [a], [b]) }}

Formats a number, a date or a text that holds one of them.
ParameterTypeDescription
format? Number or String The number of decimals, or the date format.
a? Object The thousands separator for numbers, or utc for dates.
b? String The decimal separator for numbers.

Returns String The formatted value.

The arguments depend on the value:

ValueArgumentsResult
Numberdecimals, separator, decimalSeparatorNumber format(). Without decimals, no decimals are written.
Dateformat, utcDate format()
Text with a dateformatThe date, by default as dd.MM.yyyy
Numeric textdecimals, separator, decimalSeparatorThe number, which keeps its decimals when decimals is missing

A text counts as a date when it contains a colon, a space, the letter T, or more than one - or .. null and undefined write an empty text.

Numbers
'{{ price | format(2) }}'.render({ price: 1234.5 }); // "1 234.50"
'{{ price | format }}'.render({ price: 1234.5 }); // "1 234"
'{{ price | format(2, ",", ".") }}'.render({ price: 1234.5 }); // "1,234.50"
Dates
var date = new Date(2026, 10, 18);

'{{ date | format("dd.MM.yyyy") }}'.render({ date: date }); // "18.11.2026"
'{{ date | format }}'.render({ date: date }); // "2026-11-18"
Text values
Thelpers.format('2026-11-18'); // "18.11.2026"
Thelpers.format('2026-11-18', 'yyyy/MM'); // "2026/11"
Thelpers.format('1234.5'); // "1 234.5"
Thelpers.format('1234,5', 2); // "1 234.50"
method

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

Chooses a text by the size of a number.
ParameterTypeDescription
zero String or Array The text for 0, or all four forms.
one String The text for 1.
few String The text for numbers greater than 1 and lower than 5.
other String The text for all other numbers.

Returns String The chosen text.

The helper calls pluralize(). A text value is converted to a number first; spaces are removed and a comma is read as a decimal separator.

Choose a form
var template = '{{ count | pluralize("no items", "# item", "# items", "# items") }}';

template.render({ count: 0 }); // "no items"
template.render({ count: 1 }); // "1 item"
template.render({ count: 7 }); // "7 items"
method

{{ value | raw }}

Writes the value without HTML encoding.

Returns Object The unchanged value.

Use this helper only with trusted content.

Write HTML
'{{ html | raw }}'.render({ html: '<b>Hello</b>' }); // "<b>Hello</b>"
Made in Europe