Tangular
The Tangular template engine: compile and render templates and use its helpers.
Tangular is the template engine of Total.js client libraries, and the framework loads it at start as the globals Tangular and Ta. The CMS compiler uses it for navigation templates, and you can use it for e-mails, small fragments or text files.
var template = Tangular.compile('<h2>{{ title }}</h2>{{ if items.length }}<ul>{{ foreach m in items }}<li>{{ $index + 1 }}. {{ m.name }} - {{ m.price | format(2) }}</li>{{ end }}</ul>{{ else }}<p>Empty</p>{{ fi }}');
var html = template({ title: 'Products', items: [{ name: 'Mug', price: 9.5 }] });| Syntax | Meaning |
|---|---|
{{ name }} | Writes an encoded value of the model |
{{ value | helper(args) }} | Pipes the value through helpers |
{{ $.key }} | Reads the second argument (repository) |
{{ if cond }}...{{ else if cond }}...{{ else }}...{{ fi }} | Condition |
{{ foreach m in items }}...{{ end }} | Loop over an array or the keys of an object, $index is the index |
{{ break }}, {{ continue }} | Loop control |
Built-in helpers are encode, raw, format, pluralize, empty (def) and currency. Register your own with Tangular.register(name, fn), or pass private helpers as the third argument of the compiled function:
Tangular.register('upper', function(value) {
return (value + '').toUpperCase();
});
var fn = Tangular.compile('{{ name | upper }} {{ name | stars }}');
fn({ name: 'peter' }, null, { stars: function(value) { return '*' + value + '*'; } });Values are encoded by default; use raw only for HTML you trust.