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

# Tangular

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

## API

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

Type: `method`

Skompiluje a vyrenderuje šablónu Tangular v jednom kroku.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `template` | `String` | yes |  | Šablóna. |
| `model` | `Object` | no | `{}` | Dáta. |
| `repository` | `Object` | no |  | Ďalšie dáta dostupné ako `$`. |
| `helpers` | `Object` | no |  | Súkromné pomocné funkcie pre toto volanie. |

Returns: `String` Výstup.

Framework pri štarte načíta Tangular (v5) ako globálne `Tangular` a `Ta`. V šablóne `{{ name }}` vypíše zakódovanú hodnotu z modelu, `{{ $.key }}` načíta repository a `{{ value | helper(args) }}` pošle hodnotu cez pomocné funkcie. Keď renderuješ tú istú šablónu viackrát, použi `compile()`.

Example: Renderovanie
```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`

Skompiluje šablónu do opakovane použiteľnej funkcie.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `template` | `String` | yes |  | Šablóna. |
| `tagbeg` | `String` | no | `{{` | Otvárací tag (zdroj regulárneho výrazu). |
| `tagend` | `String` | no | `}}` | Zatvárací tag. |

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

Funkcia má signatúru `fn(model, [repository], [helpers])`. Chyby v šablóne sa vypíšu do konzoly a funkcia vráti `undefined`. `tagbeg` a `tagend` nahrádzajú `{{` a `}}`; používajú sa v regulárnom výraze, preto escapuj špeciálne znaky.

Example: Opakované použitie šablóny
```js
var row = Tangular.compile('<tr><td>{{ name }}</td><td>{{ price | format(2) }}</td></tr>');
var html = products.map(p => row(p)).join('');
```

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

### Tangular.register(name, fn)

Type: `method`

Zaregistruje globálnu pomocnú funkciu.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Názov pomocnej funkcie. |
| `fn` | `Function(value, [a], [b], ...)` | yes |  | Pomocná funkcia. |

Returns: `Object` `Tangular`.

Pomocná funkcia dostane poslanú hodnotu ako prvý argument a za ňou argumenty pomocnej funkcie; `this` je model. To isté ako `Thelpers[name] = fn`.

Example: Pomocná funkcia pre veľké písmená
```js
Tangular.register('upper', function(value) {
	return (value + '').toUpperCase();
});

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

### Thelpers

Type: `property`

Objekt globálnych pomocných funkcií Tangular.

Returns: `Object`

Dostupný aj ako `Tangular.helpers`. Vstavané pomocné funkcie: `encode`, `raw`, `format`, `pluralize`, `empty` (alias `def`) a `currency`. Hodnota bez pomocnej funkcie prejde cez `encode`; chýbajúca pomocná funkcia vypíše varovanie a vráti hodnotu.

Example: Pridanie pomocnej funkcie
```js
Thelpers.yesno = function(value) {
	return value ? 'Yes' : 'No';
};
```

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

Type: `method`

Zakóduje hodnotu pre HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | Hodnota. |
| `type` | `String|Function` | no |  | Kódovanie. |

Returns: `String`

Bez `type` escapuje `&`, `<`, `>` a `"`. Typy: `json`, `json2` (odsadený JSON), `url` alebo `urlencoded`, `querify` alebo funkcia, ktorá kódovanie vykoná.

Example: JSON v atribúte
```html
<div data-model="{{ item | encode('json') | encode }}"></div>
```

### Thelpers.raw(value)

Type: `method`

Vypíše hodnotu bez zakódovania.

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

Returns: `Object`

Používaj ju iba pre dôveryhodné HTML.

Example: Dôveryhodné HTML
```html
<div>{{ body | raw }}</div>
```

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

Type: `method`

Formátuje číslo alebo dátum.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|Date|String` | yes |  | Hodnota. |
| `format` | `Number|String` | no |  | Desatinné miesta pre čísla, vzor pre dátumy. |
| `a` | `String` | no |  | Oddeľovač tisícov pre čísla. |
| `b` | `String` | no |  | Desatinný oddeľovač pre čísla. |

Returns: `String`

Čísla volajú `Number.format(decimals, separator, decimalSeparator)`, dátumy volajú `Date.format(format)`. Reťazec, ktorý vyzerá ako dátum, sa sparsuje a naformátuje (predvolene `dd.MM.yyyy`), ostatné reťazce sa parsujú ako čísla. `null` vypíše prázdny reťazec. Číslo `0` sa formátuje ako akékoľvek iné číslo.

Example: Čísla a dátumy
```html
<td>{{ price | format(2) }}</td>
<td>{{ dtcreated | format('yyyy-MM-dd HH:mm') }}</td>
```

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

Type: `method`

Vyberie text podľa čísla.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|String` | yes |  | Číslo. |
| `zero` | `String` | yes |  | Text pre 0. |
| `one` | `String` | yes |  | Text pre 1. |
| `few` | `String` | yes |  | Text pre 2 až 4. |
| `other` | `String` | yes |  | Text pre ostatné čísla. |

Returns: `String`

Volá `Number.pluralize()`. `#` v texte sa nahradí číslom.

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

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

Type: `method`

Vypíše predvolený text pre prázdnu hodnotu.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | Hodnota. |
| `def` | `String` | no | `---` | Text pre prázdnu hodnotu. |

Returns: `String`

Neprázdna hodnota sa zakóduje. Alias je `def`.

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

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

Type: `method`

Naformátuje číslo ako menu.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number|String` | yes |  | Suma. |
| `currency` | `String` | no |  | Kľúč v `DEF.currencies`. |

Returns: `String`

Volá `Number.currency(currency)`, ktorá používa formátovače registrované v `DEF.currencies` (kľúč `default`, keď je `currency` prázdne). Bez formátovača vypíše číslo s dvoma desatinnými miestami. Ostatné typy vypíšu prázdny reťazec.

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`

Prevedie objekt na pole `{ key, value }`.

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

Returns: `Array`

`foreach` ju používa, keď je hodnota objekt, takže môžeš prechádzať cyklom cez kľúče objektu.

Example: Cyklus cez objekt
```html
{{ foreach m in headers }}<b>{{ m.key }}</b>: {{ m.value }}<br />{{ end }}
```
