Mustache library
Contents
Mustache library allows you to render templates using the Mustache templating language!
To start using this library, add the following into your build.gradle
file:
dependencies {
include 'com.enonic.lib:lib-mustache:2.0.0'
}
Usage
To use this library in your JavaScript code, it first needs to be required:
var mustacheLib = require('/lib/mustache');
Then, you will need to resolve your template/view.
var view = resolve('view/fruit.html');
Template:
<div>
{{#fruits}}
<div>
Name:
<div>{{name}}</div>
Color:
<div>{{color}}</div>
</div>
{{/fruits}}
</div>
All the variables used in the template/view must be gathered into a JSON object:
var model = {
fruits: [
{
name: 'Apple',
color: 'Red'
},
{
name: 'Pear',
color: 'Green'
}
]
};
To process template:
var result = mustacheLib.render(view, model);
Output:
<div>
<div>
Name:
<div>Apple</div>
Color:
<div>Red</div>
</div>
<div>
Name:
<div>Pear</div>
Color:
<div>Green</div>
</div>
</div>