-
Notifications
You must be signed in to change notification settings - Fork 0
Documentation
var simplet = require('simplet');
simplet(config)
config: object
simpleT uses a configuration object which is used to set up the template engine. The configuration is applied on all templates rendered using this instance of simpleT. Example:
var engine = simplet({
cache: false, // Cache the generated code, default is true
close: '}}', // The close tag of the templates, default is '%>'
open: '{{', // The open tag of the templates, default is '<%'
string: true, // Specifies if the provided source is a string or the path of a file, default is false (file path)
raw: true // Specifies that the engine will return the executable content of the template, not the result, default is false
});
.render(source[, imports, id])
source: string
imports: object
id: string
The .render()
method is used to render the provided templates, these templates can be as strings or filepaths, provided in the source parameter
, simpleT will know how to use the depending on the string attribute in the config object of the engine. It is possible to define some special values inside the template by adding these ass attributes to the imports object. The last parameter is used as identification for caching the template only if the first parameter is used as the whole template string. Example:
engine.render('{{="Hello World"}}', {
hello: 'hello',
world: 'world'
}, 'HelloWorld');
The syntax below will be defined only for default open and close tags.
To isolate the code from the rest of the content of the template the open and the close tags are used, example:
<% if (true) { %>
<h1>Hello World</h1>
<% } %>
To print some data it is necessary to use the open tag followed by an =
symbol, the data and the close tag or using the print()
function inside the code isolation, example:
<%= 'HelloWorld'.toLowerCase() %>
or
<% print('HelloWorld'.toLowerCase()) %>
To insert another template inside the current template it is necessary to use the open tag followed by an #
symbol, the path to the template and the close tag or using the include()
function inside the code isolation, example:
<%# 'header.ejs' %>
or
<% include('header.ejs') %>
On the client-side simpleT can be used with utils/simplet.js
file inside the module folder. The only difference from the server-side version is that instead of files HTML elements are used and their id should be provided. Example:
<script src="../utils/simplet.js"></script>
<script id="include" type="text/simplet">
<%= add + 1 %><br>
</script>
<script id="template" type="text/simplet">
1<br>
<%# 'include', {add: 1} %>
<% print(3) %>
</script>
<script>
window.onload = function () {
var engine = simplet({});
document.getElementById('result').innerHTML = engine.render('template', {});
}
// 'result' is the id of an HTML element in which will be added the result content of the template
</script>