Tangular
Tangular.compile(), Tangular.render(), Tangular.register() e Tangular.helpers.
In questa pagina
API
method
Tangular.compile(template, [tagbeg], [tagend])
Compila un template Tangular in una funzione.
Tangular.compile(template, [tagbeg], [tagend])| Parametro | Tipo | Descrizione |
|---|---|---|
template |
String |
Il template. |
tagbeg? |
String |
Inizio di un comando. Predefinito: '{{' |
tagend? |
String |
Fine di un comando. Predefinito: '}}' |
Restituisce Function function(model, [repository], [helpers]).
La funzione è template(model, [repository], [helpers]) e restituisce una stringa. I nomi nel template vengono letti dal modello, $ è il repository e $index l'indice in un ciclo. Gli errori durante il rendering vengono stampati nella console. tagbeg e tagend sostituiscono {{ e }} (come parti di un'espressione regolare). I comandi sono descritti in View.
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])
Compila ed esegue il rendering di un template in una sola chiamata.
Tangular.render(template, [model], [repository], [helpers])| Parametro | Tipo | Descrizione |
|---|---|---|
template |
String |
Il template. |
model? |
Object |
Il modello. Predefinito: {} |
repository? |
Object |
Disponibile come $. |
helpers? |
Object |
Helper per questa chiamata. |
Restituisce String L'output.
Usa Tangular.compile() per i template di cui esegui spesso il rendering.
Tangular.render('Hello {{ name }}!', { name: 'Peter' }); // 'Hello Peter!'
method
Tangular.register(name, fn)
Aggiunge un helper per tutti i template Tangular.
Tangular.register(name, fn)| Parametro | Tipo | Descrizione |
|---|---|---|
name |
String |
Nome dell'helper. |
fn |
Function |
function(value, [args...]). |
Restituisce Object Tangular, per il concatenamento.
L'helper viene chiamato come fn.call(model, value, ...arguments): {{ price | money('EUR') }} chiama money(price, 'EUR'). Un helper mancante stampa un avviso e restituisce il valore.
Tangular.register('money', function(value, currency) {
return value.format(2) + ' ' + (currency || 'EUR');
});
property
Tangular.helpers
Gli helper integrati di Tangular, disponibili anche come variabile globale Thelpers.
Tangular.helpersRestituisce Object
| Helper | Descrizione |
|---|---|
encode([type]) | Codifica in HTML; usato per ogni valore senza raw. Tipi: json, json2, url, querify |
raw | Il valore senza codifica |
format([format], ...) | Formatta un numero o una data (le stringhe vengono riconosciute) |
currency([code]) | Formatta un numero con un formatter da DEF.currencies[code] (o DEF.currencies.default); senza di esso usa due decimali |
pluralize(zero, one, few, other) | Sceglie una parola in base a un numero |
empty([text]), def([text]) | Il valore, o text (predefinito ---) quando è vuoto |
Tangular.render('{{ count | pluralize(\'no items\', \'# item\', \'# items\', \'# items\') }}', { count: 3 });