Skip to content

Mustache Library

Diego Pasten edited this page Oct 19, 2016 · 3 revisions

This library enables the use of Mustache as a templating language. It's using JMustache under the hood so it can have variations from the spec. To start using the library, update your build to include the following dependency:

dependencies {
  compile "io.purplejs:purplejs-mustache:${purplejs.version}"
}

Using

This library only consists of one method called render. It takes two parameters: the view and the model. The model is supposed to be a Javascript object. Here's an example that renders a list of fruits:

// Require the library.
var mustache = require('/lib/mustache');

// Resolve the view (relative to this script).
var view = resolve('./test.html');

// Create the model.
var model = {
  fruits: [{
    name: 'Apple',
    color: 'Red'
  }, {
    name: 'Pear',
    color: 'Green'
  }]
};

// Render the view with supplied model.
var result = mustache.render(view, model);

Here's the Mustache template:

<div>
  {{#fruits}}
  <div>
    Name:
    <div>{{name}}</div>
    Color:
    <div>{{color}}</div>
  </div>
  {{/fruits}}
</div>
Clone this wiki locally