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

# Tangular

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

## API

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

Type: `method`

Compiles and renders a Tangular template in one step.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `template` | `String` | yes |  | The template. |
| `model` | `Object` | no | `{}` | The data. |
| `repository` | `Object` | no |  | Extra data available as `$`. |
| `helpers` | `Object` | no |  | Private helpers for this call. |

Returns: `String` The output.

The framework loads Tangular (v5) at start as the globals `Tangular` and `Ta`. In the template, `{{ name }}` writes an encoded value of the model, `{{ $.key }}` reads the repository, `{{ value | helper(args) }}` pipes the value through helpers. Use `compile()` when you render the same template more times.

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`

Compiles a template into a reusable function.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `template` | `String` | yes |  | The template. |
| `tagbeg` | `String` | no | `{{` | The opening tag (regular expression source). |
| `tagend` | `String` | no | `}}` | The closing tag. |

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

The function has the signature `fn(model, [repository], [helpers])`. Errors in the template are written to the console and the function returns `undefined`. `tagbeg` and `tagend` replace `{{` and `}}`; they are used in a regular expression, so escape special characters.

Example: Reuse a 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: Own tags
```js
var fn = Tangular.compile('Hello [[ name ]]', '\\[\\[', '\\]\\]');
fn({ name: 'Peter' });
```

### Tangular.register(name, fn)

Type: `method`

Registers a global helper.

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

Returns: `Object` `Tangular`.

The helper gets the piped value as the first argument and the helper arguments after it; `this` is the model. The same as `Thelpers[name] = fn`.

Example: Upper-case helper
```js
Tangular.register('upper', function(value) {
	return (value + '').toUpperCase();
});

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

### Thelpers

Type: `property`

The object of global Tangular helpers.

Returns: `Object`

Also available as `Tangular.helpers`. Built-in helpers: `encode`, `raw`, `format`, `pluralize`, `empty` (alias `def`) and `currency`. A value without a helper is passed through `encode`; a missing helper writes a warning and returns the value.

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

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

Type: `method`

Encodes a value for HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The value. |
| `type` | `String|Function` | no |  | The encoding. |

Returns: `String`

Without `type` it escapes `&`, `<`, `>` and `"`. Types: `json`, `json2` (indented JSON), `url` or `urlencoded`, `querify`, or a function that does the encoding.

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

### Thelpers.raw(value)

Type: `method`

Writes the value without encoding.

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

Returns: `Object`

Use it only for trusted HTML.

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

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

Type: `method`

Formats a number or a date.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|Date|String` | yes |  | The value. |
| `format` | `Number|String` | no |  | Decimals for numbers, a pattern for dates. |
| `a` | `String` | no |  | The thousands separator for numbers. |
| `b` | `String` | no |  | The decimal separator for numbers. |

Returns: `String`

Numbers call `Number.format(decimals, separator, decimalSeparator)`, dates call `Date.format(format)`. A string that looks like a date is parsed and formatted (default `dd.MM.yyyy`), other strings are parsed as numbers. `null` writes an empty string. The number `0` is formatted like any other number.

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

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

Type: `method`

Chooses a text by a number.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|String` | yes |  | The number. |
| `zero` | `String` | yes |  | Text for 0. |
| `one` | `String` | yes |  | Text for 1. |
| `few` | `String` | yes |  | Text for 2 to 4. |
| `other` | `String` | yes |  | Text for other numbers. |

Returns: `String`

Calls `Number.pluralize()`. `#` in the text is replaced by the number.

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

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

Type: `method`

Writes a default text for an empty value.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | The value. |
| `def` | `String` | no | `---` | The text for an empty value. |

Returns: `String`

A non-empty value is encoded. The alias is `def`.

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

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

Type: `method`

Formats a number as a currency.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|String` | yes |  | The amount. |
| `currency` | `String` | no |  | The key in `DEF.currencies`. |

Returns: `String`

Calls `Number.currency(currency)`, which uses formatters registered in `DEF.currencies` (the `default` key when `currency` is empty). Without a formatter it writes the number with two decimals. Other types write an empty string.

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`

Converts an object to an array of `{ key, value }`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `obj` | `Object` | yes |  | The object. |

Returns: `Array`

`foreach` uses it when the value is an object, so you can loop over the keys of an object.

Example: Loop over an object
```html
{{ foreach m in headers }}<b>{{ m.key }}</b>: {{ m.value }}<br />{{ end }}
```
