Spanish Total.js
English
Documentation For AI agents
Contents

Tangular

Tangular.compile(), Tangular.render(), Tangular.register() and Tangular.helpers.

.md GitHub
On this page

API

method

Tangular.compile(template, [tagbeg], [tagend])

Compiles a Tangular template into a function.
ParameterTypeDescription
template String The template.
tagbeg? String Start of a command. Default: '{{'
tagend? String End of a command. Default: '}}'

Returns Function function(model, [repository], [helpers]).

The function is template(model, [repository], [helpers]) and returns a string. Names in the template are read from the model, $ is the repository and $index the index in a loop. Errors while rendering are printed to the console. tagbeg and tagend replace {{ and }} (as parts of a regular expression). Commands are described in Views.

Compile once
var row = Tangular.compile('<tr><td>{{ name }}</td><td>{{ price | format(2) }}</td></tr>');
var html = items.map(item => row(item)).join('');
Local helpers
var t = Tangular.compile('{{ name | badge }}');
t({ name: 'New' }, null, { badge: function(value) { return '<span class="badge">' + value + '</span>'; } });
method

Tangular.render(template, [model], [repository], [helpers])

Compiles and renders a template in one call.
ParameterTypeDescription
template String The template.
model? Object The model. Default: {}
repository? Object Available as $.
helpers? Object Helpers for this call.

Returns String The output.

Use Tangular.compile() for templates that you render often.

Render
Tangular.render('Hello {{ name }}!', { name: 'Peter' }); // 'Hello Peter!'
method

Tangular.register(name, fn)

Adds a helper for all Tangular templates.
ParameterTypeDescription
name String Helper name.
fn Function function(value, [args...]).

Returns Object Tangular, for chaining.

The helper is called as fn.call(model, value, ...arguments): {{ price | money('EUR') }} calls money(price, 'EUR'). A missing helper prints a warning and returns the value.

Helper
Tangular.register('money', function(value, currency) {
	return value.format(2) + ' ' + (currency || 'EUR');
});
property

Tangular.helpers

Built-in helpers of Tangular, also available as the global Thelpers.

Returns Object

HelperDescription
encode([type])HTML-encodes; used for every value without raw. Types: json, json2, url, querify
rawThe value without encoding
format([format], ...)Formats a number or a date (strings are detected)
currency([code])Formats a number with a formatter from DEF.currencies[code] (or DEF.currencies.default); without one it uses two decimals
pluralize(zero, one, few, other)Chooses a word by a number
empty([text]), def([text])The value, or text (default ---) when it is empty
Usage
Tangular.render('{{ count | pluralize(\'no items\', \'# item\', \'# items\', \'# items\') }}', { count: 3 });
Made in Europe