Tangular
Tangular.compile(), Tangular.render(), Tangular.register() and Tangular.helpers.
On this page
API
method
Tangular.compile(template, [tagbeg], [tagend])
Compiles a Tangular template into a function.
Tangular.compile(template, [tagbeg], [tagend])| Parameter | Type | Description |
|---|---|---|
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.
var row = Tangular.compile('<tr><td>{{ name }}</td><td>{{ price | format(2) }}</td></tr>');
var html = items.map(item => row(item)).join('');
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.
Tangular.render(template, [model], [repository], [helpers])| Parameter | Type | Description |
|---|---|---|
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.
Tangular.render('Hello {{ name }}!', { name: 'Peter' }); // 'Hello Peter!'
method
Tangular.register(name, fn)
Adds a helper for all Tangular templates.
Tangular.register(name, fn)| Parameter | Type | Description |
|---|---|---|
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.
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.
Tangular.helpersReturns Object
| Helper | Description |
|---|---|
encode([type]) | HTML-encodes; used for every value without raw. Types: json, json2, url, querify |
raw | The 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 |
Tangular.render('{{ count | pluralize(\'no items\', \'# item\', \'# items\', \'# items\') }}', { count: 3 });