Drafts Reference

MustacheTemplate

The MustacheTemplate object support rendering of templates using the Mustache template style.

Mustache templates offer advanced features for iterating over items, creating conditional blocks of text and more. This is still a bit of an experimental feature, please send feedback if you are finding edge cases or are interested in more functionality in this area.

The object can be used in one of two ways, by passing a specific template as a string and rendering it, or by passing a path to a subdirectory of the iCloud Drive/Drafts/Library/Templates folder which can contain more than one Mustache style templates (with file extension “.mustache”), and then rendering them. The late method has the advantage of supporting the use of partial templates in the same folder.

For details on using Mustache templates, we recommend reviewing tutorials.

About Passing Data to Templates

When rendering Mustache templates, you pass the template itself and a data object which contains the values available to insert. The data object should be a Javascript object with keys and values. Values can be basic data types (numbers, strings, dates) and also arrays or nested objects which can be iterated using conventions of the Mustache syntax.

Instance Functions

Instance Properties

Class Functions

Examples

// create template to loop over drafts
let t = `Template Output:
{{#drafts}}
Draft: {{content}}
{{#isFlagged}}Flagged!{{/isFlagged}}{{^isFlagged}}Not Flagged{{/isFlagged}}
{{/drafts}}`;

let d1 = Draft.create();
d1.content = "First draft";
d1.isFlagged = true;
let d2 = Draft.create();
d2.content = "Second draft";
let drafts = [d1, d2];

let data = {
  "drafts": drafts
};

let template = MustacheTemplate.createWithTemplate(t);
let result = template.render(data);

/* RESULTS...

Template Output:

D: First draft
Flagged!

D: Second draft
Not Flagged

*/