Tangular
Tangular.render(), Tangular.compile(), Tangular.register(), Thelpers e Tangular.toArray().
In questa pagina
API
method
Tangular.render(template, [model], [repository], [helpers])
Compila e renderizza un template Tangular in un solo passaggio.
Tangular.render(template, [model], [repository], [helpers])| Parametro | Tipo | Descrizione |
|---|---|---|
template |
String |
Il template. |
model? |
Object |
I dati. Predefinito: {} |
repository? |
Object |
Dati aggiuntivi disponibili come $. |
helpers? |
Object |
Helper privati per questa chiamata. |
Restituisce String L'output.
Il framework carica Tangular (v5) all'avvio come variabili globali Tangular e Ta. Nel template, {{ name }} scrive un valore codificato del modello, {{ $.key }} legge il repository, {{ value | helper(args) }} passa il valore attraverso degli helper. Usa compile() quando renderizzi lo stesso template più volte.
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])
Compila un template in una funzione riutilizzabile.
Tangular.compile(template, [tagbeg], [tagend])| Parametro | Tipo | Descrizione |
|---|---|---|
template |
String |
Il template. |
tagbeg? |
String |
Il tag di apertura (sorgente di un'espressione regolare). Predefinito: {{ |
tagend? |
String |
Il tag di chiusura. Predefinito: }} |
Restituisce Function(model, [repository], [helpers])
La funzione ha la firma fn(model, [repository], [helpers]). Gli errori nel template vengono scritti nella console e la funzione restituisce undefined. tagbeg e tagend sostituiscono {{ e }}; vengono usati in un'espressione regolare, quindi esegui l'escape dei caratteri speciali.
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)
Registra un helper globale.
Tangular.register(name, fn)| Parametro | Tipo | Descrizione |
|---|---|---|
name |
String |
Il nome dell'helper. |
fn |
Function(value, [a], [b], ...) |
L'helper. |
Restituisce Object Tangular.
L'helper riceve il valore passato come primo argomento e gli argomenti dell'helper dopo di esso; this è il modello. Uguale a Thelpers[name] = fn.
Tangular.register('upper', function(value) {
return (value + '').toUpperCase();
});
Tangular.render('{{ name | upper }}', { name: 'peter' }); // PETER
property
Thelpers
L'oggetto degli helper globali di Tangular.
ThelpersRestituisce Object
Disponibile anche come Tangular.helpers. Helper integrati: encode, raw, format, pluralize, empty (alias def) e currency. Un valore senza helper passa attraverso encode; un helper mancante scrive un avviso e restituisce il valore.
Thelpers.yesno = function(value) {
return value ? 'Yes' : 'No';
};
method
Thelpers.encode(value, [type])
Codifica un valore per HTML.
Thelpers.encode(value, [type])| Parametro | Tipo | Descrizione |
|---|---|---|
value |
Object |
Il valore. |
type? |
String|Function |
La codifica. |
Restituisce String
Senza type esegue l'escape di &, <, > e ". Tipi: json, json2 (JSON indentato), url o urlencoded, querify, oppure una funzione che esegue la codifica.
<div data-model="{{ item | encode('json') | encode }}"></div>
method
Thelpers.raw(value)
Scrive il valore senza codifica.
Thelpers.raw(value)| Parametro | Tipo | Descrizione |
|---|---|---|
value |
Object |
Il valore. |
Restituisce Object
Usalo solo per HTML affidabile.
<div>{{ body | raw }}</div>
method
Thelpers.format(value, [format], [a], [b])
Formatta un numero o una data.
Thelpers.format(value, [format], [a], [b])| Parametro | Tipo | Descrizione |
|---|---|---|
value |
Number|Date|String |
Il valore. |
format? |
Number|String |
Decimali per i numeri, un pattern per le date. |
a? |
String |
Il separatore delle migliaia per i numeri. |
b? |
String |
Il separatore decimale per i numeri. |
Restituisce String
I numeri chiamano Number.format(decimals, separator, decimalSeparator), le date chiamano Date.format(format). Una stringa che sembra una data viene analizzata e formattata (predefinito dd.MM.yyyy), le altre stringhe vengono analizzate come numeri. null scrive una stringa vuota. Il numero 0 viene formattato come qualsiasi altro numero.
<td>{{ price | format(2) }}</td>
<td>{{ dtcreated | format('yyyy-MM-dd HH:mm') }}</td>
method
Thelpers.pluralize(value, zero, one, few, other)
Sceglie un testo in base a un numero.
Thelpers.pluralize(value, zero, one, few, other)| Parametro | Tipo | Descrizione |
|---|---|---|
value |
Number|String |
Il numero. |
zero |
String |
Testo per 0. |
one |
String |
Testo per 1. |
few |
String |
Testo per da 2 a 4. |
other |
String |
Testo per gli altri numeri. |
Restituisce String
Chiama Number.pluralize(). # nel testo viene sostituito dal numero.
{{ count | pluralize('no items', '# item', '# items', '# items') }}
method
Thelpers.empty(value, [def])
Scrive un testo predefinito per un valore vuoto.
Thelpers.empty(value, [def])| Parametro | Tipo | Descrizione |
|---|---|---|
value |
Object |
Il valore. |
def? |
String |
Il testo per un valore vuoto. Predefinito: --- |
Restituisce String
Un valore non vuoto viene codificato. L'alias è def.
<td>{{ phone | empty('not set') }}</td>
method
Thelpers.currency(value, [currency])
Formatta un numero come valuta.
Thelpers.currency(value, [currency])| Parametro | Tipo | Descrizione |
|---|---|---|
value |
Number|String |
L'importo. |
currency? |
String |
La chiave in DEF.currencies. |
Restituisce String
Chiama Number.currency(currency), che usa i formattatori registrati in DEF.currencies (la chiave default quando currency è vuoto). Senza formattatore scrive il numero con due decimali. Gli altri tipi scrivono una stringa vuota.
DEF.currencies.eur = function(val) {
return val.format(2) + ' EUR';
};
Tangular.render('{{ price | currency(\'eur\') }}', { price: 10 }); // 10.00 EUR
method
Tangular.toArray(obj)
Converte un oggetto in un array di { key, value }.
Tangular.toArray(obj)| Parametro | Tipo | Descrizione |
|---|---|---|
obj |
Object |
L'oggetto. |
Restituisce Array
foreach la usa quando il valore è un oggetto, così puoi ciclare sulle chiavi di un oggetto.
{{ foreach m in headers }}<b>{{ m.key }}</b>: {{ m.value }}<br />{{ end }}