Helper di Tangular
Gli helper disponibili in ogni template Tangular per codificare, formattare e leggere gli elenchi di codici.
In questa pagina
Un helper segue il valore in un template, come in {{ price | format(2) }}, e gli helper possono essere concatenati. Un valore senza helper viene codificato con encode. Per scrivere template e registrare helper personalizzati, vedi Template con Tangular.
API
method
{{ id | cl(name, [key]) }}
Scrive un campo di un elemento di un elenco di codici.
{{ id | cl(name, [key]) }}| Parametro | Tipo | Descrizione |
|---|---|---|
name |
String |
Il nome dell'elenco di codici. |
key? |
String |
Il campo da scrivere. Predefinito: name |
Restituisce String Il valore del campo.
L'elenco di codici è l'array in DEF.cl[name] e l'elemento viene trovato tramite il suo id. Quando non viene trovato nulla, l'helper scrive DEF.empty, oppure una stringa vuota quando key è impostato.
DEF.cl.colors = [{ id: 2, name: 'Red', code: 'R' }];
console.log(Tangular.render("{{ id | cl('colors') }}", { id: 2 })); // Red
console.log(Tangular.render("{{ id | cl('colors', 'code') }}", { id: 2 })); // R
console.log(Tangular.render("{{ id | cl('colors') }}", { id: 9 })); // ---
method
{{ id | clcolor(name, [key]) }}
Scrive un elemento di un elenco di codici come etichetta colorata.
{{ id | clcolor(name, [key]) }}| Parametro | Tipo | Descrizione |
|---|---|---|
name |
String |
Il nome dell'elenco di codici. |
key? |
String |
Il campo da scrivere. Predefinito: name |
Restituisce String L'HTML dell'etichetta.
L'helper scrive uno <span class="ui-clcolor"> con il colore dell'elemento come sfondo, l'icona dell'elemento e il campo. Scrive una stringa vuota quando l'elemento non viene trovato. L'output non viene codificato.
DEF.cl.colors = [{ id: 2, name: 'Red', icon: 'ti ti-circle', color: '#f00' }];
console.log(Tangular.render("{{ id | clcolor('colors') }}", { id: 2 })); // <span class="ui-clcolor" style="background:#f00"><i class="ti ti-circle mr5"></i>Red</span>
method
{{ id | clicon(name, [key]) }}
Scrive un elemento di un elenco di codici con la sua icona.
{{ id | clicon(name, [key]) }}| Parametro | Tipo | Descrizione |
|---|---|---|
name |
String |
Il nome dell'elenco di codici. |
key? |
String |
Il campo da scrivere. Predefinito: name |
Restituisce String L'HTML dell'elemento.
L'icona assume il colore dell'elemento. Un elemento senza icona scrive solo il campo, e un elemento non trovato scrive una stringa vuota. L'output non viene codificato.
DEF.cl.colors = [{ id: 1, name: 'Plain' }, { id: 2, name: 'Red', icon: 'ti ti-circle', color: '#f00' }];
console.log(Tangular.render("{{ id | clicon('colors') }}", { id: 2 })); // <i class="ti ti-circle mr5" style="color:#f00"></i>Red
console.log(Tangular.render("{{ id | clicon('colors') }}", { id: 1 })); // Plain
method
{{ value | currency([name]) }}
Formatta un numero o una stringa numerica con una valuta registrata.
{{ value | currency([name]) }}| Parametro | Tipo | Descrizione |
|---|---|---|
name? |
String |
Il nome della valuta. Predefinito: DEF.currency |
Restituisce String Il prezzo formattato.
L'helper usa currency(). Un valore di un altro tipo scrive una stringa vuota.
DEF.currencies.eur = value => value.format(2) + ' EUR';
console.log(Tangular.render("{{ price | currency('eur') }}", { price: 5 })); // 5.00 EUR
console.log(Tangular.render("{{ price | currency('eur') }}", { price: '7.5' })); // 7.50 EUR
method
{{ value | def([text], [raw]) }}
Un alias dell'helper empty.
{{ value | def([text], [raw]) }}| Parametro | Tipo | Descrizione |
|---|---|---|
text? |
String |
Il testo per un valore vuoto. Predefinito: DEF.empty |
raw? |
Boolean |
true scrive il valore senza codificarlo. Predefinito: false |
Restituisce String Il valore o il testo.
console.log(Tangular.render("{{ name | def('n/a') }}", { name: null })); // n/a
method
{{ value | empty([text], [raw]) }}
Scrive un testo predefinito per un valore vuoto.
{{ value | empty([text], [raw]) }}| Parametro | Tipo | Descrizione |
|---|---|---|
text? |
String |
Il testo per un valore vuoto. Predefinito: DEF.empty |
raw? |
Boolean |
true scrive il valore senza codificarlo. Predefinito: false |
Restituisce String Il valore o il testo.
Ogni valore falsy è considerato vuoto, compreso 0. Gli altri valori vengono codificati a meno che raw non sia true. Passare true come primo argomento attiva anche raw.
console.log(Tangular.render('{{ name | empty }}', { name: '' })); // ---
console.log(Tangular.render("{{ name | empty('n/a') }}", { name: '' })); // n/a
console.log(Tangular.render('{{ name | empty }}', { name: '<b>' })); // <b>
console.log(Tangular.render('{{ name | empty(true) }}', { name: '<b>' })); // <b>
method
{{ value | encode([type]) }}
Codifica un valore per l'output.
{{ value | encode([type]) }}| Parametro | Tipo | Descrizione |
|---|---|---|
type? |
String |
json per JSON, json2 per JSON indentato, url o urlencoded per encodeURIComponent() e querify per una stringa di query. |
Restituisce String Il valore codificato.
Senza type, l'helper esegue l'escape di <, >, & e ", e scrive una stringa vuota per null e undefined.
console.log(Tangular.render('{{ value | encode }}', { value: '<b>' })); // <b>
console.log(Tangular.render("{{ value | encode('json') }}", { value: { a: 1 } })); // {"a":1}
console.log(Tangular.render("{{ value | encode('url') }}", { value: 'a b' })); // a%20b
console.log(Tangular.render("{{ value | encode('querify') }}", { value: { a: 1, b: 'x y' } })); // ?a=1&b=x%20y
method
{{ value | format([format], [separator], [decimalSeparator]) }}
Formatta un numero o una data.
{{ value | format([format], [separator], [decimalSeparator]) }}| Parametro | Tipo | Descrizione |
|---|---|---|
format? |
Number or String |
Le cifre decimali per un numero, oppure il layout per una data. |
separator? |
String |
Il separatore delle migliaia di un numero. |
decimalSeparator? |
String |
Il separatore decimale di un numero. |
Restituisce String Il valore formattato.
Per un numero, gli argomenti sono quelli di format(), e un numero senza decimals viene scritto senza la parte decimale. Per un Date, il primo argomento è il layout di format().
Una stringa che contiene una data viene prima convertita e usa dd.MM.yyyy per impostazione predefinita, mentre una stringa che contiene un numero viene convertita in numero. null e undefined scrivono una stringa vuota.
console.log(Tangular.render('{{ value | format(2) }}', { value: 1234.5 })); // 1 234.50
console.log(Tangular.render("{{ value | format(2, ',', '.') }}", { value: 1234.5 })); // 1,234.50
console.log(Tangular.render('{{ value | format }}', { value: 1234.5 })); // 1 234
var date = new Date(2026, 10, 8, 15, 4);
console.log(Tangular.render("{{ value | format('dd.MM.yyyy HH:mm') }}", { value: date })); // 08.11.2026 15:04
console.log(Tangular.render('{{ value | format }}', { value: '2026-11-08' })); // 08.11.2026
method
{{ value | pluralize(zero, one, few, other) }}
Seleziona il testo che corrisponde a un numero.
{{ value | pluralize(zero, one, few, other) }}| Parametro | Tipo | Descrizione |
|---|---|---|
zero |
String |
Il testo per 0. |
one |
String |
Il testo per 1. |
few |
String |
Il testo per i numeri da 2 a 4. |
other |
String |
Il testo per gli altri numeri. |
Restituisce String Il testo selezionato.
Gli argomenti sono quelli di pluralize(). Una stringa numerica viene convertita in numero, e un valore vuoto conta come 0.
var template = "{{ count | pluralize('no items', '# item', '# items', '# items') }}";
console.log(Tangular.render(template, { count: 3 })); // 3 items
console.log(Tangular.render(template, { count: 1 })); // 1 item
console.log(Tangular.render(template, { count: null })); // no items
method
{{ value | raw }}
Scrive un valore senza codificarlo.
{{ value | raw }}Restituisce Object Il valore invariato.
Usalo solo per contenuti che possono essere inseriti come HTML in sicurezza.
console.log(Tangular.render('{{ value | raw }}', { value: '<b>Hi</b>' })); // <b>Hi</b>
console.log(Tangular.render('{{ value }}', { value: '<b>Hi</b>' })); // <b>Hi</b>