Spanish Total.js
English
Documentation For AI agents
Contents

Tangular

The functions that compile and render templates and register helpers.

.md GitHub
On this page

Tangular is the template engine included in the library, and Ta is another name of the same object. The syntax of the templates is explained in the guide Templates with Tangular, and the helpers available in every template are listed in Tangular helpers.

API

method

Tangular.compile(template, [begin], [end])

Compiles a template to a function.
ParameterTypeDescription
template String The template.
begin? String The text that opens a tag. Default: {{
end? String The text that closes a tag. Default: }}

Returns Function A function (model, [repository], [helpers]) that returns the rendered text.

The returned function takes the same arguments as Tangular.render(), without the template. begin and end are used only together. They are plain text, and a value with a backslash is taken as a part of a regular expression.

An error while a template renders is written to the console and the function returns undefined. An error in the syntax of the template is thrown while it is compiled.

Render several times
var template = Tangular.compile('Hello {{ name }}!');

console.log(template({ name: 'Ann' })); // Hello Ann!
console.log(template({ name: 'Bob' })); // Hello Bob!
Other delimiters
var template = Tangular.compile('[[ name ]] and {{ other }}', '[[', ']]');

console.log(template({ name: 'Ann' })); // Ann and {{ other }}
property

Tangular.helpers

The helpers available in every template, by name.

Returns Object The helpers available in every template, by name.

The global variable Thelpers is the same object. A function assigned to it is a helper, in the same way as with Tangular.register(). The helpers can also be called from code.

Add and call a helper
Thelpers.initials = function(value) {
	return value.split(' ').map(word => word.charAt(0)).join('');
};

console.log(Tangular.render('{{ name | initials }}', { name: 'Ann Lee' })); // AL
console.log(Thelpers.encode('<b>')); // &lt;b&gt;
console.log(Thelpers === Tangular.helpers); // true
method

Tangular.register(name, callback)

Registers a helper for all templates.
ParameterTypeDescription
name String The name used in the templates.
callback Function A function that receives the value and the arguments written in the template, and returns the text to write.

Returns Object The Tangular object, so calls can be chained.

Inside the callback, this is the model. The result is not encoded.

Register a helper
Tangular.register('wrap', function(value, before, after) {
	return before + value + after;
});

console.log(Tangular.render('{{ name | wrap("[", "]") }}', { name: 'Ann' })); // [Ann]
console.log(Tangular.render('{{ wrap("a", "b", "c") }}')); // bac
method

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

Compiles a template and renders it.
ParameterTypeDescription
template String The template.
model? Object The values available in the template by name. Default: {}
repository? Object More values, available as $. Default: {}
helpers? Object Helpers for this call. They have priority over the registered ones.

Returns String The rendered text, or undefined after an error.

The compiled template is kept in memory, so rendering the same text again does not compile it again.

Render a template
console.log(Tangular.render('Hello {{ name }}', { name: 'Ann' })); // Hello Ann
console.log(Tangular.render('{{ $.title }}: {{ name }}', { name: 'Ann' }, { title: 'User' })); // User: Ann
Helpers for one call
var helpers = { shout: value => value + '!' };

console.log(Tangular.render('{{ name | shout }}', { name: 'Ann' }, null, helpers)); // Ann!
property

Tangular.version

The version of the template engine.

Returns String The version of the template engine.

Read the version
console.log(Tangular.version); // v5.0.6
Made in Europe