https://mustache.github.io/mustache.5.html
mustache(5) - Logic-less templates.
mustache(5) Mustache Manual mustache(5) NAME mustache - Logic-less templates. SYNOPSIS A typical Mustache template: Hello {{name}} You have just won {{value}} dollars! {{#in_ca}} Well, {{taxed_value}} dollars, after taxes. {{/in_ca}} Given the following ha
mustache.github.io
- NAME
- mustache - Logic-less templates.
TAG TYPES
- Variables : {{name}}, find the name key in the current context.
- If there is no name key, the parent contexts will be checked recursively. not found, nothing will be rendered.
- HTML escaped default
- for return unescaped HTML : {{{name}}} or {{& name}}
- ex)
- * {{name}}
* {{age}}
* {{company}}
* {{{company}}} - Hash: {
"name": "Chris",
"company": "<b>GitHub</b>"
} - Output:
* Chris
*
* <b>GitHub</b>
* <b>GitHub</b>
- * {{name}}
- Sections : {{#person}}{{/person}}
- render blocks of text one or more times, depending on the value of the key in the current context.
- The behavior of the section is determined by the value of the key.
- False Values or Empty Lists
- If has a value of false or an empty list, will not be displayed.
- ex)
- Shown.
{{#person}}
Never shown!
{{/person}} - Hash:{
"person": false
} - Output:
Shown.
- Shown.
- Non-Empty Lists
- will be rendered and displayed one or more times.
- will be displayed once for each item in the list. for each iteration. loop over collections.
- ex)
{{#repo}}
<b>{{name}}</b>
{{/repo}} - Hash:{
"repo": [
{ "name": "resque" },
{ "name": "hub" },
{ "name": "rip" }
]
}
Output:
<b>resque</b>
<b>hub</b>
<b>rip</b>
- Lambdas
- When the value is a callable object, such as a function or lambda
- the object will be invoked and passed the block of text.
- The text passed is the literal block, unrendered.
- {{tags}} will not have been expanded - the lambda should do that on its own.
- you can implement filters or caching.
- ex)
{{#wrapped}}
{{name}} is awesome.
{{/wrapped}}
Hash:{
"name": "Willy",
"wrapped": function() {
return function(text, render) {
return "<b>" + render(text) + "</b>"
}
}
}
Output:
<b>Willy is awesome.</b>
- Non-False Values
- When the value is non-false but not a list, it will be used as the context for a single rendering of the block.
- ex)
{{#person?}}
Hi {{name}}!
{{/person?}}
Hash:{
"person?": { "name": "Jon" }
}
Output:
Hi Jon!
- render blocks of text one or more times, depending on the value of the key in the current context.
- Inverted Sections : {{^person}}{{/person}}
- will be rendered if the key doesn't exist, is false, or is an empty list.
- ex)
{{#repo}}
<b>{{name}}</b>
{{/repo}}
{{^repo}}
No repos :(
{{/repo}}
Hash:{
"repo": []
}
Output:
No repos :(
- Comments : {{! ignore me }}.
- Comments begin with a bang and are ignored.
- ex) <h1>Today{{! ignore me }}.</h1>
- result : <h1>Today.</h1>
- Partial : {{> box}}
- rendered at runtime (as opposed to compile time), so recursive partials are possible.
- They also inherit the calling context.
- ex ) [ERB](http://en.wikipedia.org/wiki/ERuby) file
- formal : <%= partial :next_more, :start => start, :size => size %>
- Mustache : {{> next_more}}
- the next_more, mustache file will inherit the size and start methods from the calling context.
- partials as includes, imports, template expansion, nested templates, or subtemplates, even though those aren't literally the case here.
- ex)
- base.mustache:
<h2>Names</h2>
{{#names}}
{{> user}}
{{/names}} - user.mustache:
<strong>{{name}}</strong> - Can be thought of as a single, expanded template:
<h2>Names</h2>
{{#names}}
<strong>{{name}}</strong>
{{/names}}
- base.mustache:
- Set Delimiter
- Set Delimiter tags start with an equal sign and change the tag delimiters from {{ and }} to custom strings.
- 1.uses the default tag style,
- * {{default_tags}}
{{=<% %>=}}
- * {{default_tags}}
- 2. uses erb style as defined by the Set Delimiter tag
- * <% erb_style_tags %>
<%={{ }}=%> - 3. returns to the default style after yet another Set Delimiter
- * {{ default_tags_again }}
- "is useful for languages like TeX, where double-braces may occur in the text and are awkward to use for markup."
- Custom delimiters may not contain whitespace or the equals sign.
- * <% erb_style_tags %>