Spanish Total.js
Italiano
Documentazione Per agenti IA
Indice

Tangular

Tangular.render(), Tangular.compile(), Tangular.register(), Thelpers e Tangular.toArray().

.md GitHub
In questa pagina

API

method

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

Compila e renderizza un template Tangular in un solo passaggio.
ParametroTipoDescrizione
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.

Render
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.
ParametroTipoDescrizione
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.

Riutilizzare un template
var row = Tangular.compile('<tr><td>{{ name }}</td><td>{{ price | format(2) }}</td></tr>');
var html = products.map(p => row(p)).join('');
Tag personalizzati
var fn = Tangular.compile('Hello [[ name ]]', '\\[\\[', '\\]\\]');
fn({ name: 'Peter' });
method

Tangular.register(name, fn)

Registra un helper globale.
ParametroTipoDescrizione
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.

Helper per il maiuscolo
Tangular.register('upper', function(value) {
	return (value + '').toUpperCase();
});

Tangular.render('{{ name | upper }}', { name: 'peter' }); // PETER
property

Thelpers

L'oggetto degli helper globali di Tangular.

Restituisce 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.

Aggiungere un helper
Thelpers.yesno = function(value) {
	return value ? 'Yes' : 'No';
};
method

Thelpers.encode(value, [type])

Codifica un valore per HTML.
ParametroTipoDescrizione
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.

JSON in un attributo
<div data-model="{{ item | encode('json') | encode }}"></div>
method

Thelpers.raw(value)

Scrive il valore senza codifica.
ParametroTipoDescrizione
value Object Il valore.

Restituisce Object

Usalo solo per HTML affidabile.

HTML affidabile
<div>{{ body | raw }}</div>
method

Thelpers.format(value, [format], [a], [b])

Formatta un numero o una data.
ParametroTipoDescrizione
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.

Numeri e date
<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.
ParametroTipoDescrizione
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.

Elementi
{{ count | pluralize('no items', '# item', '# items', '# items') }}
method

Thelpers.empty(value, [def])

Scrive un testo predefinito per un valore vuoto.
ParametroTipoDescrizione
value Object Il valore.
def? String Il testo per un valore vuoto. Predefinito: ---

Restituisce String

Un valore non vuoto viene codificato. L'alias è def.

Predefinito
<td>{{ phone | empty('not set') }}</td>
method

Thelpers.currency(value, [currency])

Formatta un numero come valuta.
ParametroTipoDescrizione
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.

Euro
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 }.
ParametroTipoDescrizione
obj Object L'oggetto.

Restituisce Array

foreach la usa quando il valore è un oggetto, così puoi ciclare sulle chiavi di un oggetto.

Ciclo su un oggetto
{{ foreach m in headers }}<b>{{ m.key }}</b>: {{ m.value }}<br />{{ end }}
Fatto in Europa