Tangular
Tangular.render(), Tangular.compile(), Tangular.register(), Thelpers and Tangular.toArray().
On this page
API
method
Tangular.render(template, [model], [repository], [helpers])
Compiles and renders a Tangular template in one step.
Tangular.render(template, [model], [repository], [helpers])| Parameter | Type | Description |
|---|---|---|
template |
String |
The template. |
model? |
Object |
The data. Default: {} |
repository? |
Object |
Extra data available as $. |
helpers? |
Object |
Private helpers for this call. |
Returns String The output.
The framework loads Tangular (v5) at start as the globals Tangular and Ta. In the template, {{ name }} writes an encoded value of the model, {{ $.key }} reads the repository, {{ value | helper(args) }} pipes the value through helpers. Use compile() when you render the same template more times.
var html = Tangular.render('<h1>{{ name }}</h1>{{ foreach m in items }}<b>{{ m.name }}</b>{{ end }}', { name: 'Products', items: [{ name: 'A' }, { name: 'B' }] });
method
Tangular.compile(template, [tagbeg], [tagend])
Compiles a template into a reusable function.
Tangular.compile(template, [tagbeg], [tagend])| Parameter | Type | Description |
|---|---|---|
template |
String |
The template. |
tagbeg? |
String |
The opening tag (regular expression source). Default: {{ |
tagend? |
String |
The closing tag. Default: }} |
Returns Function(model, [repository], [helpers])
The function has the signature fn(model, [repository], [helpers]). Errors in the template are written to the console and the function returns undefined. tagbeg and tagend replace {{ and }}; they are used in a regular expression, so escape special characters.
var row = Tangular.compile('<tr><td>{{ name }}</td><td>{{ price | format(2) }}</td></tr>');
var html = products.map(p => row(p)).join('');
var fn = Tangular.compile('Hello [[ name ]]', '\\[\\[', '\\]\\]');
fn({ name: 'Peter' });
method
Tangular.register(name, fn)
Registers a global helper.
Tangular.register(name, fn)| Parameter | Type | Description |
|---|---|---|
name |
String |
The helper name. |
fn |
Function(value, [a], [b], ...) |
The helper. |
Returns Object Tangular.
The helper gets the piped value as the first argument and the helper arguments after it; this is the model. The same as Thelpers[name] = fn.
Tangular.register('upper', function(value) {
return (value + '').toUpperCase();
});
Tangular.render('{{ name | upper }}', { name: 'peter' }); // PETER
property
Thelpers
The object of global Tangular helpers.
ThelpersReturns Object
Also available as Tangular.helpers. Built-in helpers: encode, raw, format, pluralize, empty (alias def) and currency. A value without a helper is passed through encode; a missing helper writes a warning and returns the value.
Thelpers.yesno = function(value) {
return value ? 'Yes' : 'No';
};
method
Thelpers.encode(value, [type])
Encodes a value for HTML.
Thelpers.encode(value, [type])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The value. |
type? |
String|Function |
The encoding. |
Returns String
Without type it escapes &, <, > and ". Types: json, json2 (indented JSON), url or urlencoded, querify, or a function that does the encoding.
<div data-model="{{ item | encode('json') | encode }}"></div>
method
Thelpers.raw(value)
Writes the value without encoding.
Thelpers.raw(value)| Parameter | Type | Description |
|---|---|---|
value |
Object |
The value. |
Returns Object
Use it only for trusted HTML.
<div>{{ body | raw }}</div>
method
Thelpers.format(value, [format], [a], [b])
Formats a number or a date.
Thelpers.format(value, [format], [a], [b])| Parameter | Type | Description |
|---|---|---|
value |
Number|Date|String |
The value. |
format? |
Number|String |
Decimals for numbers, a pattern for dates. |
a? |
String |
The thousands separator for numbers. |
b? |
String |
The decimal separator for numbers. |
Returns String
Numbers call Number.format(decimals, separator, decimalSeparator), dates call Date.format(format). A string that looks like a date is parsed and formatted (default dd.MM.yyyy), other strings are parsed as numbers. null writes an empty string. The number 0 is formatted like any other number.
<td>{{ price | format(2) }}</td>
<td>{{ dtcreated | format('yyyy-MM-dd HH:mm') }}</td>
method
Thelpers.pluralize(value, zero, one, few, other)
Chooses a text by a number.
Thelpers.pluralize(value, zero, one, few, other)| Parameter | Type | Description |
|---|---|---|
value |
Number|String |
The number. |
zero |
String |
Text for 0. |
one |
String |
Text for 1. |
few |
String |
Text for 2 to 4. |
other |
String |
Text for other numbers. |
Returns String
Calls Number.pluralize(). # in the text is replaced by the number.
{{ count | pluralize('no items', '# item', '# items', '# items') }}
method
Thelpers.empty(value, [def])
Writes a default text for an empty value.
Thelpers.empty(value, [def])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The value. |
def? |
String |
The text for an empty value. Default: --- |
Returns String
A non-empty value is encoded. The alias is def.
<td>{{ phone | empty('not set') }}</td>
method
Thelpers.currency(value, [currency])
Formats a number as a currency.
Thelpers.currency(value, [currency])| Parameter | Type | Description |
|---|---|---|
value |
Number|String |
The amount. |
currency? |
String |
The key in DEF.currencies. |
Returns String
Calls Number.currency(currency), which uses formatters registered in DEF.currencies (the default key when currency is empty). Without a formatter it writes the number with two decimals. Other types write an empty string.
DEF.currencies.eur = function(val) {
return val.format(2) + ' EUR';
};
Tangular.render('{{ price | currency(\'eur\') }}', { price: 10 }); // 10.00 EUR
method
Tangular.toArray(obj)
Converts an object to an array of { key, value }.
Tangular.toArray(obj)| Parameter | Type | Description |
|---|---|---|
obj |
Object |
The object. |
Returns Array
foreach uses it when the value is an object, so you can loop over the keys of an object.
{{ foreach m in headers }}<b>{{ m.key }}</b>: {{ m.value }}<br />{{ end }}