@zokugun/template v0.3.0
@zokugun/template
It is a fast template engine forked from doT with extended and extendable tags.
Getting Started
In Node.js
With node previously installed:
npm install @zokugun/template
Use it with JavaScript
:
require('kaoscript/register');
const { Template } = require('@zokugun/template')();
const myTemplate = template.compile(`It's nice to meet you, {{:it}}.`);
console.log(myTemplate('miss White'));
Use it with kaoscript
:
import '@zokugun/template'
const myTemplate = template.compile(`It's nice to meet you, {{:it}}.`)
console.log(myTemplate('miss White'))
Differences from doT
{{ }}
has been changed to {{| |}}
to be able to do:
{{|
function hello(name) {
if(arguments.length) {
return 'hello ' + name;
}
else {
return 'hello world!';
}
};
|}}
{{:hello('foo')}}
API
import '@zokugun/template'
The variable template
is the default template compiler. It contains the tags described above.
The class Template
allows to create new templates. It has the following API:
new Template(tags, options)
The default compiler contains the extra method new which allows you to create new template compiler.
The arguments tags and options can be optionals. By default, options will be:
{
varnames: 'it',
strip: true, // remove spaces in javascript code. Be careful of missing ;
append: true // use string concatenation or addition assignment operator
}
Example:
const custom = new Template({
interpolate: {
regex: /\$\{([\s\S]+?)\}/g
replace(m, code) => this.cse.start + 'it.' + code + this.cse.end
}
})
const hello = custom.compile('Hello ${firstname}')
console.log(hello({
firstname: 'John'
lastname: 'Doe'
}))
template.addTag(name, regex, replace)
The method addTag allow you to add new tag so he can extends the compiler. The tags are executed in alphabetic order. So its name will determine when the tag will be executed.
The function replace will be called as in str.replace(regex, replace) excepted that the variable this will an object like:
{
cse: {
start: string, // start of the code
end: string, // end of the code
startencode: string, // start of the code that will be HTML escaped
endencode: string // end of the code that will be HTML escaped
},
unescape(str), // unescape the code to pass from the template to the function's code
sid: integer // the sid for the variables' names
}
template.clearTags()
The method clearTags removes all the tags defined in the compiler.
template.compile(template, options)
The function compile returns a function based of the string template. The argument options will overwrite the default options of the compiler.
The first line of the template can also contains options for the compiler. It must start with '{{}} ' and the options separated with spaces.
{{}} strip:true
hello {{:it.firstname}}
{{}} strip:false varnames:firstname,lastname
hello {{:firstname}}
template.removeTag(name)
The method clearTags removes the tag named name.
template.run(template, variables, options)
The method run will firstly compiles the template with the options and the variables' names. Then it will execute the resulting function with the variables.
console.log(template.run('It\'s nice to meet you, {{:name}}.', {
name: 'miss White'
}))
This is the least efficient to use a template. Because the template will be compiled every time.
Tags
Interpolation
Interpolation with encoding
Evaluation
Conditionals
Array Iteration
Object Iteration
Iterator
Range Iteration
Partials
Blocks
Comments
Escape
Forked from
License
MIT © Baptiste Augrain
5 years ago