Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
416 lines (308 sloc) 10.4 KB
<!DOCTYPE html>
<meta http-equiv='content-type' value='text/html;charset=utf8'>
<meta name='generator' value='Ronn/v0.5'>
<title>mustache(5) -- Logic-less templates.</title>
<style type='text/css'>
body {margin:0}
#man, #man code, #man pre, #man tt, #man kbd, #man samp {
background:#fff; }
#man { max-width:89ex; text-align:justify; margin:0 25px 25px 25px }
#man h1, #man h2, #man h3 { color:#232221;clear:left }
#man h1 { font-size:28px; margin:15px 0 30px 0; text-align:center }
#man h2 { font-size:18px; margin-bottom:0; margin-top:10px; line-height:1.3; }
#man h3 { font-size:16px; margin:0 0 0 4ex; }
#man p, #man ul, #man ol, #man dl, #man pre { margin:0 0 18px 0; }
#man pre {
padding:5px 7px;
margin:0px 0 20px 0;
border-left:2ex solid #ddd}
#man pre + h2, #man pre + h3 {
#man h2 + pre, #man h3 + pre {
#man > p, #man > ul, #man > ol, #man > dl, #man > pre { margin-left:8ex; }
#man dt { margin:0; clear:left }
#man dt.flush { float:left; width:8ex }
#man dd { margin:0 0 0 9ex }
#man code, #man strong, #man b { font-weight:bold; color:#131211; }
#man pre code { font-weight:normal; color:#232221; background:inherit }
#man em, var, u {
font-style:normal; color:#333231; border-bottom:1px solid #999; }
#man { display:none; }
#man, #man li { margin:2px 0 10px 0; padding:0;
float:left; width:33%; list-style-type:none;
text-transform:uppercase; font-size:18px; color:#999;
#man { width:100%; }
#man { text-align:left }
#man { text-align:center;letter-spacing:4px }
#man { text-align:right }
#man a { color:#999 }
#man a:hover { color:#333231 }
<div id='man'>
<h1 class='man-title'>mustache(5)</h1>
<ol class='head man'>
<li class='tl'>mustache(5)</li>
<li class='tc'>Mustache Manual</li>
<li class='tr'>mustache(5)</li>
<h2 id='NAME'>NAME</h2>
<p><code>mustache</code> -- Logic-less templates.</p>
<p>A typical Mustache template:</p>
<pre><code>Hello {{name}}
You have just won ${{value}}!
Well, ${{taxed_value}}, after taxes.
<p>Given the following hash:</p>
"name": "Chris",
"value": 10000,
"taxed_value": 10000 - (10000 * 0.4),
"in_ca": true
<p>Will produce the following:</p>
<pre><code>Hello Chris
You have just won $10000!
Well, $6000.0, after taxes.
<p>Mustache can be used for HTML, config files, source code -
anything. It works by expanding tags in a template using values
provided in a hash or object.</p>
<p>We call it "logic-less" because there are no if statements, else
clauses, or for loops. Instead there are only tags. Some tags are
replaced with a value, some nothing, and others a series of
values. This document explains the different types of Mustache tags.</p>
<h2>TAG TYPES</h2>
<p>Tags are indicated by the double mustaches. <code>{{person}}</code> is a tag, as
is <code>{{#person}}</code>. In both examples, we'd refer to <code>person</code> as the key
or tag key. Let's talk about the different types of tags.</p>
<p>The most basic tag type is the variable. A <code>{{name}}</code> tag in a basic
template will try to find the <code>name</code> key in the current context. If
there is no <code>name</code> key, nothing will be rendered.</p>
<p>All variables are HTML escaped by default. If you want to return
unescaped HTML, use the triple mustache: <code>{{{name}}}</code>.</p>
<p>You can also use <code>&amp;</code> to unescape a variable: <code>{{&amp; name}}</code>. This may be
useful when changing delimiters (see "Set Delimiter" below).</p>
<p>By default a variable "miss" returns an empty string. This can usually
be configured in your Mustache library. The Ruby version of Mustache
supports raising an exception in this situation, for instance.</p>
<pre><code>* {{name}}
* {{age}}
* {{company}}
* {{{company}}}
"name": "Chris",
"company": "&lt;b&gt;GitHub&lt;/b&gt;"
<pre><code>* Chris
* &amp;lt;b&amp;gt;GitHub&amp;lt;/b&amp;gt;
* &lt;b&gt;GitHub&lt;/b&gt;
<p>Sections render blocks of text one or more times, depending on the
value of the key in the current context.</p>
<p>A section begins with a pound and ends with a slash. That is,
<code>{{#person}}</code> begins a "person" section while <code>{{/person}}</code> ends it.</p>
<p>The behavior of the section is determined by the value of the key.</p>
<p><strong>False Values or Empty Lists</strong></p>
<p>If the <code>person</code> key exists and has a value of false or an empty
list, the HTML between the pound and slash will not be displayed.</p>
Never shown!
"person": true,
<p><strong>Non-Empty Lists</strong></p>
<p>If the <code>person</code> key exists and has a non-false value, the HTML between
the pound and slash will be rendered and displayed one or more times.</p>
<p>When the value is a non-empty list, the text in the block will be
displayed once for each item in the list. The context of the block
will be set to the current item for each iteration. In this way we can
loop over collections.</p>
"repo": [
{ "name": "resque" },
{ "name": "hub" },
{ "name": "rip" },
<p>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. <code>{{tags}}</code> will not have been expanded
- the lambda should do that on its own. In this way you can implement
filters or caching.</p>
{{name}} is awesome.
"name": "Willy",
"wrapped": function() {
return function(text) {
return "&lt;b&gt;" + render(text) + "&lt;/b&gt;"
<pre><code>&lt;b&gt;Willy is awesome.&lt;/b&gt;
<p><strong>Non-False Values</strong></p>
<p>When the value is non-false but not a list, it will be used as the
context for a single rendering of the block.</p>
Hi {{name}}!
"person?": { "name": "Jon" }
<pre><code>Hi Jon!
<h3>Inverted Sections</h3>
<p>An inverted section begins with a caret (hat) and ends with a
slash. That is <code>{{^person}}</code> begins a "person" inverted section while
<code>{{/person}}</code> ends it.</p>
<p>While sections can be used to render text one or more times based on the
value of the key, inverted sections may render text once based
on the inverse value of the key. That is, they will be rendered
if the key doesn't exist, is false, or is an empty list.</p>
No repos :(
"repo": []
<pre><code>No repos :(
<p>Comments begin with a bang and are ignored. The following template:</p>
<pre><code>&lt;h1&gt;Today{{! ignore me }}.&lt;/h1&gt;
<p>Will render as follows:</p>
<p>Comments may contain newlines.</p>
<p>Partials begin with a greater than sign, like <code>{{&gt; box}}</code>.</p>
<p>Partials are rendered at runtime (as opposed to compile time), so
recursive partials are possible. Just avoid infinite loops.</p>
<p>They also inherit the calling context. Whereas in ERB you may have
<pre><code>&lt;%= partial :next_more, :start =&gt; start, :size =&gt; size %&gt;
<p>Mustache requires only this:</p>
<pre><code>{{&gt; next_more}}
<p>Why? Because the <code>next_more.mustache</code> file will inherit the <code>size</code> and
<code>start</code> methods from the calling context.</p>
<p>In this way you may want to think of partials as includes, or template
expansion, even though it's not literally true.</p>
<p>For example, this template and partial:</p>
{{&gt; user}}
<p>Can be thought of as a single, expanded template:</p>
<h3>Set Delimiter</h3>
<p>Set Delimiter tags start with an equal sign and change the tag
delimiters from <code>{{</code> and <code>}}</code> to custom strings.</p>
<p>Consider the following contrived example:</p>
<pre><code>* {{default_tags}}
{{=&lt;% %>=}}
* &lt;% erb_style_tags %>
&lt;%={{ }}=%>
* {{ default_tags_again }}
<p>Here we have a list with three items. The first item uses the default
tag style, the second uses erb style as defined by the Set Delimiter
tag, and the third returns to the default style after yet another Set
Delimiter declaration.</p>
<p>According to <a href="">ctemplates</a>, this "is useful for languages like TeX, where
double-braces may occur in the text and are awkward to use for
<p>Custom delimiters may not contain whitespace or the equals sign.</p>
<p>Mustache is Copyright (C) 2009 Chris Wanstrath</p>
<p>Original CTemplate by Google</p>
<h2>SEE ALSO</h2>
<p>mustache(1), mustache(7),
<a href=""></a></p>
<ol class='foot man'>
<li class='tl'>DEFUNKT</li>
<li class='tc'>April 2010</li>
<li class='tr'>mustache(5)</li>
Jump to Line
Something went wrong with that request. Please try again.