---
title: Tangular
project: Total.js V4
version: 0.0.99
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/total-js-v4/0-0-99/reference/tangular/
updated: 2026-11-18T16:57:49.579Z
---

# Tangular

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

## API

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

Type: `method`

Compila e renderizza un template Tangular in un solo passaggio.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `template` | `String` | yes |  | Il template. |
| `model` | `Object` | no | `{}` | I dati. |
| `repository` | `Object` | no |  | Dati aggiuntivi disponibili come `$`. |
| `helpers` | `Object` | no |  | Helper privati per questa chiamata. |

Returns: `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.

Example: Render
```js
var html = Tangular.render('<h1>{{ name }}</h1>{{ foreach m in items }}<b>{{ m.name }}</b>{{ end }}', { name: 'Products', items: [{ name: 'A' }, { name: 'B' }] });
```

### Tangular.compile(template, [tagbeg], [tagend])

Type: `method`

Compila un template in una funzione riutilizzabile.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `template` | `String` | yes |  | Il template. |
| `tagbeg` | `String` | no | `{{` | Il tag di apertura (sorgente di un'espressione regolare). |
| `tagend` | `String` | no | `}}` | Il tag di chiusura. |

Returns: `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.

Example: Riutilizzare un template
```js
var row = Tangular.compile('<tr><td>{{ name }}</td><td>{{ price | format(2) }}</td></tr>');
var html = products.map(p => row(p)).join('');
```

Example: Tag personalizzati
```js
var fn = Tangular.compile('Hello [[ name ]]', '\\[\\[', '\\]\\]');
fn({ name: 'Peter' });
```

### Tangular.register(name, fn)

Type: `method`

Registra un helper globale.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'helper. |
| `fn` | `Function(value, [a], [b], ...)` | yes |  | L'helper. |

Returns: `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`.

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

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

### Thelpers

Type: `property`

L'oggetto degli helper globali di Tangular.

Returns: `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.

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

### Thelpers.encode(value, [type])

Type: `method`

Codifica un valore per HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | Il valore. |
| `type` | `String|Function` | no |  | La codifica. |

Returns: `String`

Senza `type` esegue l'escape di `&`, `<`, `>` e `"`. Tipi: `json`, `json2` (JSON indentato), `url` o `urlencoded`, `querify`, oppure una funzione che esegue la codifica.

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

### Thelpers.raw(value)

Type: `method`

Scrive il valore senza codifica.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | Il valore. |

Returns: `Object`

Usalo solo per HTML affidabile.

Example: HTML affidabile
```html
<div>{{ body | raw }}</div>
```

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

Type: `method`

Formatta un numero o una data.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|Date|String` | yes |  | Il valore. |
| `format` | `Number|String` | no |  | Decimali per i numeri, un pattern per le date. |
| `a` | `String` | no |  | Il separatore delle migliaia per i numeri. |
| `b` | `String` | no |  | Il separatore decimale per i numeri. |

Returns: `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.

Example: Numeri e date
```html
<td>{{ price | format(2) }}</td>
<td>{{ dtcreated | format('yyyy-MM-dd HH:mm') }}</td>
```

### Thelpers.pluralize(value, zero, one, few, other)

Type: `method`

Sceglie un testo in base a un numero.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|String` | yes |  | Il numero. |
| `zero` | `String` | yes |  | Testo per 0. |
| `one` | `String` | yes |  | Testo per 1. |
| `few` | `String` | yes |  | Testo per da 2 a 4. |
| `other` | `String` | yes |  | Testo per gli altri numeri. |

Returns: `String`

Chiama `Number.pluralize()`. `#` nel testo viene sostituito dal numero.

Example: Elementi
```html
{{ count | pluralize('no items', '# item', '# items', '# items') }}
```

### Thelpers.empty(value, [def])

Type: `method`

Scrive un testo predefinito per un valore vuoto.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | Il valore. |
| `def` | `String` | no | `---` | Il testo per un valore vuoto. |

Returns: `String`

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

Example: Predefinito
```html
<td>{{ phone | empty('not set') }}</td>
```

### Thelpers.currency(value, [currency])

Type: `method`

Formatta un numero come valuta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|String` | yes |  | L'importo. |
| `currency` | `String` | no |  | La chiave in `DEF.currencies`. |

Returns: `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.

Example: Euro
```js
DEF.currencies.eur = function(val) {
	return val.format(2) + ' EUR';
};

Tangular.render('{{ price | currency(\'eur\') }}', { price: 10 }); // 10.00 EUR
```

### Tangular.toArray(obj)

Type: `method`

Converte un oggetto in un array di `{ key, value }`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `obj` | `Object` | yes |  | L'oggetto. |

Returns: `Array`

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

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