arrow-down

Mustache library

Contents

xp 7.+ blue

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>

API

The following function is defined in this library.

render

Renders a template by expanding its tags using the provided values.

Parameters

  • view (object) Location of the template. Use resolve(..) to resolve a template.

  • model (object) Model used to render the template.


Contents

Contents

AI-powered search

Juke AI