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

# Tangular

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

## API

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

Type: `method`

Compila y renderiza una plantilla de Tangular en un solo paso.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `template` | `String` | yes |  | La plantilla. |
| `model` | `Object` | no | `{}` | Los datos. |
| `repository` | `Object` | no |  | Datos extra disponibles como `$`. |
| `helpers` | `Object` | no |  | Helpers privados para esta llamada. |

Returns: `String` La salida.

El framework carga Tangular (v5) al arrancar como las globales `Tangular` y `Ta`. En la plantilla, `{{ name }}` escribe un valor codificado del modelo, `{{ $.key }}` lee el repositorio y `{{ value | helper(args) }}` pasa el valor por helpers. Usa `compile()` cuando renderizas la misma plantilla varias veces.

Example: Renderizar
```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 una plantilla en una función reutilizable.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `template` | `String` | yes |  | La plantilla. |
| `tagbeg` | `String` | no | `{{` | La etiqueta de apertura (código de expresión regular). |
| `tagend` | `String` | no | `}}` | La etiqueta de cierre. |

Returns: `Function(model, [repository], [helpers])`

La función tiene la firma `fn(model, [repository], [helpers])`. Los errores de la plantilla se escriben en la consola y la función devuelve `undefined`. `tagbeg` y `tagend` reemplazan `{{` y `}}`; se usan en una expresión regular, así que escapa los caracteres especiales.

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

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

### Tangular.register(name, fn)

Type: `method`

Registra un helper global.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | El nombre del helper. |
| `fn` | `Function(value, [a], [b], ...)` | yes |  | El helper. |

Returns: `Object` `Tangular`.

El helper recibe el valor pasado como primer argumento y los argumentos del helper después; `this` es el modelo. Lo mismo que `Thelpers[name] = fn`.

Example: Helper de mayúsculas
```js
Tangular.register('upper', function(value) {
	return (value + '').toUpperCase();
});

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

### Thelpers

Type: `property`

El objeto de helpers globales de Tangular.

Returns: `Object`

También disponible como `Tangular.helpers`. Helpers integrados: `encode`, `raw`, `format`, `pluralize`, `empty` (alias `def`) y `currency`. Un valor sin helper pasa por `encode`; un helper que falta escribe un aviso y devuelve el valor.

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

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

Type: `method`

Codifica un valor para HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | El valor. |
| `type` | `String|Function` | no |  | La codificación. |

Returns: `String`

Sin `type`, escapa `&`, `<`, `>` y `"`. Tipos: `json`, `json2` (JSON con sangría), `url` o `urlencoded`, `querify`, o una función que hace la codificación.

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

### Thelpers.raw(value)

Type: `method`

Escribe el valor sin codificar.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | El valor. |

Returns: `Object`

Úsalo solo para HTML de confianza.

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

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

Type: `method`

Formatea un número o una fecha.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|Date|String` | yes |  | El valor. |
| `format` | `Number|String` | no |  | Decimales para los números, un patrón para las fechas. |
| `a` | `String` | no |  | El separador de miles para los números. |
| `b` | `String` | no |  | El separador decimal para los números. |

Returns: `String`

Los números llaman a `Number.format(decimals, separator, decimalSeparator)` y las fechas a `Date.format(format)`. Un string que parece una fecha se analiza y se formatea (por defecto `dd.MM.yyyy`); los demás strings se analizan como números. `null` escribe un string vacío. El número `0` se formatea como cualquier otro número.

Example: Números y fechas
```html
<td>{{ price | format(2) }}</td>
<td>{{ dtcreated | format('yyyy-MM-dd HH:mm') }}</td>
```

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

Type: `method`

Elige un texto según un número.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|String` | yes |  | El número. |
| `zero` | `String` | yes |  | Texto para 0. |
| `one` | `String` | yes |  | Texto para 1. |
| `few` | `String` | yes |  | Texto para 2 a 4. |
| `other` | `String` | yes |  | Texto para los demás números. |

Returns: `String`

Llama a `Number.pluralize()`. `#` en el texto se reemplaza por el número.

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

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

Type: `method`

Escribe un texto por defecto para un valor vacío.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | El valor. |
| `def` | `String` | no | `---` | El texto para un valor vacío. |

Returns: `String`

Un valor no vacío se codifica. El alias es `def`.

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

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

Type: `method`

Formatea un número como moneda.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|String` | yes |  | El importe. |
| `currency` | `String` | no |  | La clave en `DEF.currencies`. |

Returns: `String`

Llama a `Number.currency(currency)`, que usa los formateadores registrados en `DEF.currencies` (la clave `default` cuando `currency` está vacío). Sin formateador, escribe el número con dos decimales. Los otros tipos escriben un string vacío.

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`

Convierte un objeto en un array de `{ key, value }`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `obj` | `Object` | yes |  | El objeto. |

Returns: `Array`

`foreach` lo usa cuando el valor es un objeto, así puedes recorrer las claves de un objeto.

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