Debian Codegraph @

man/mustache.5.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv='content-type' content='text/html;charset=utf8'>
5 <meta name='generator' content='Ronn-NG/v0.9.1 (http://github.com/apjanke/ronn-ng/tree/0.9.1)'>
6 <title>mustache(5) - Logic-less templates.</title>
7 <style type='text/css' media='all'>
8 /* style: man */
9 body#manpage {margin:0}
10 .mp {max-width:100ex;padding:0 9ex 1ex 4ex}
11 .mp p,.mp pre,.mp ul,.mp ol,.mp dl {margin:0 0 20px 0}
12 .mp h2 {margin:10px 0 0 0}
13 .mp > p,.mp > pre,.mp > ul,.mp > ol,.mp > dl {margin-left:8ex}
14 .mp h3 {margin:0 0 0 4ex}
15 .mp dt {margin:0;clear:left}
16 .mp dt.flush {float:left;width:8ex}
17 .mp dd {margin:0 0 0 9ex}
18 .mp h1,.mp h2,.mp h3,.mp h4 {clear:left}
19 .mp pre {margin-bottom:20px}
20 .mp pre+h2,.mp pre+h3 {margin-top:22px}
21 .mp h2+pre,.mp h3+pre {margin-top:5px}
22 .mp img {display:block;margin:auto}
23 .mp h1.man-title {display:none}
24 .mp,.mp code,.mp pre,.mp tt,.mp kbd,.mp samp,.mp h3,.mp h4 {font-family:monospace;font-size:14px;line-height:1.42857142857143}
25 .mp h2 {font-size:16px;line-height:1.25}
26 .mp h1 {font-size:20px;line-height:2}
27 .mp {text-align:justify;background:#fff}
28 .mp,.mp code,.mp pre,.mp pre code,.mp tt,.mp kbd,.mp samp {color:#131211}
29 .mp h1,.mp h2,.mp h3,.mp h4 {color:#030201}
30 .mp u {text-decoration:underline}
31 .mp code,.mp strong,.mp b {font-weight:bold;color:#131211}
32 .mp em,.mp var {font-style:italic;color:#232221;text-decoration:none}
33 .mp a,.mp a:link,.mp a:hover,.mp a code,.mp a pre,.mp a tt,.mp a kbd,.mp a samp {color:#0000ff}
34 .mp b.man-ref {font-weight:normal;color:#434241}
35 .mp pre {padding:0 4ex}
36 .mp pre code {font-weight:normal;color:#434241}
37 .mp h2+pre,h3+pre {padding-left:0}
38 ol.man-decor,ol.man-decor li {margin:3px 0 10px 0;padding:0;float:left;width:33%;list-style-type:none;text-transform:uppercase;color:#999;letter-spacing:1px}
39 ol.man-decor {width:100%}
40 ol.man-decor li.tl {text-align:left}
41 ol.man-decor li.tc {text-align:center;letter-spacing:4px}
42 ol.man-decor li.tr {text-align:right;float:right}
43 </style>
44 </head>
45 <!--
46 The following styles are deprecated and will be removed at some point:
47 div#man, div#man ol.man, div#man ol.head, div#man ol.man.
48
49 The .man-page, .man-decor, .man-head, .man-foot, .man-title, and
50 .man-navigation should be used instead.
51 -->
52 <body id='manpage'>
53 <div class='mp' id='man'>
54
55 <div class='man-navigation' style='display:none'>
56 <a href="#NAME">NAME</a>
57 <a href="#SYNOPSIS">SYNOPSIS</a>
58 <a href="#DESCRIPTION">DESCRIPTION</a>
59 <a href="#TAG-TYPES">TAG TYPES</a>
60 <a href="#COPYRIGHT">COPYRIGHT</a>
61 <a href="#SEE-ALSO">SEE ALSO</a>
62 </div>
63
64 <ol class='man-decor man-head man head'>
65 <li class='tl'>mustache(5)</li>
66 <li class='tc'>Mustache Manual</li>
67 <li class='tr'>mustache(5)</li>
68 </ol>
69
70
71
72 <h2 id="NAME">NAME</h2>
73 <p class="man-name">
74 <code>mustache</code> - <span class="man-whatis">Logic-less templates.</span>
75 </p>
76 <h2 id="SYNOPSIS">SYNOPSIS</h2>
77
78 <p>A typical Mustache template:</p>
79
80 <pre><code>Hello {{name}}
81 You have just won {{value}} dollars!
82 {{#in_ca}}
83 Well, {{taxed_value}} dollars, after taxes.
84 {{/in_ca}}
85 </code></pre>
86
87 <p>Given the following hash:</p>
88
89 <pre><code>{
90 "name": "Chris",
91 "value": 10000,
92 "taxed_value": 10000 - (10000 * 0.4),
93 "in_ca": true
94 }
95 </code></pre>
96
97 <p>Will produce the following:</p>
98
99 <pre><code>Hello Chris
100 You have just won 10000 dollars!
101 Well, 6000.0 dollars, after taxes.
102 </code></pre>
103
104 <h2 id="DESCRIPTION">DESCRIPTION</h2>
105
106 <p>Mustache can be used for HTML, config files, source code -
107 anything. It works by expanding tags in a template using values
108 provided in a hash or object.</p>
109
110 <p>We call it "logic-less" because there are no if statements, else
111 clauses, or for loops. Instead there are only tags. Some tags are
112 replaced with a value, some nothing, and others a series of
113 values. This document explains the different types of Mustache tags.</p>
114
115 <p>The Mustache language has a <a href="https://github.com/mustache/spec">formal specification</a>. The current
116 manpage reflects version 1.3.0 of the specification, including the
117 official-but-optional extensions for lambdas and inheritance.</p>
118
119 <h2 id="TAG-TYPES">TAG TYPES</h2>
120
121 <p>Tags are indicated by the double mustaches. <code>{{person}}</code> is a tag, as
122 is <code>{{#person}}</code>. In both examples, we'd refer to <code>person</code> as the key
123 or tag key. Let's talk about the different types of tags.</p>
124
125 <h3 id="Variables">Variables</h3>
126
127 <p>The most basic tag type is the variable. A <code>{{name}}</code> tag in a basic
128 template will try to find the <code>name</code> key in the current context. If
129 there is no <code>name</code> key, the parent contexts will be checked recursively.
130 If the top context is reached and the <code>name</code> key is still not found,
131 nothing will be rendered.</p>
132
133 <p>All variables are HTML escaped by default. If you want to return raw contents
134 without escaping, use the triple mustache: <code>{{{name}}}</code>.</p>
135
136 <p>You can also use <code>&amp;</code> to return its raw contents: <code>{{&amp; name}}</code>. This may be
137 useful when changing delimiters (see "Set Delimiter" below).</p>
138
139 <p>By default a variable "miss" returns an empty string. This can usually
140 be configured in your Mustache library. The Ruby version of Mustache
141 supports raising an exception in this situation, for instance.</p>
142
143 <p>Template:</p>
144
145 <pre><code>* {{name}}
146 * {{age}}
147 * {{company}}
148 * {{{company}}}
149 </code></pre>
150
151 <p>Hash:</p>
152
153 <pre><code>{
154 "name": "Chris",
155 "company": "&lt;b&gt;GitHub&lt;/b&gt;"
156 }
157 </code></pre>
158
159 <p>Output:</p>
160
161 <pre><code>* Chris
162 *
163 * &amp;lt;b&amp;gt;GitHub&amp;lt;/b&amp;gt;
164 * &lt;b&gt;GitHub&lt;/b&gt;
165 </code></pre>
166
167 <p><strong>Dotted Names</strong></p>
168
169 <p>If the <code>name</code> contains dots, it is split on the dots to obtain multiple
170 keys. The first key is looked up in the context as described above. If it
171 is found, the next key is looked up within the previous result. This is
172 repeated until a key is not found or until the last key is found. The
173 final result is interpolated as above.</p>
174
175 <p>Template:</p>
176
177 <pre><code>* {{client.name}}
178 * {{age}}
179 * {{client.company.name}}
180 * {{{company.name}}}
181 </code></pre>
182
183 <p>Hash:</p>
184
185 <pre><code>{
186 "client": {
187 "name": "Chris &amp; Friends",
188 "age": 50
189 },
190 "company": {
191 "name": "&lt;b&gt;GitHub&lt;/b&gt;"
192 }
193 }
194 </code></pre>
195
196 <p>Output:</p>
197
198 <pre><code>* Chris &amp;amp; Friends
199 *
200 *
201 * &lt;b&gt;GitHub&lt;/b&gt;
202 </code></pre>
203
204 <p><strong>Implicit Iterator</strong></p>
205
206 <p>As a special case, if the <code>name</code> consists of only a dot and nothing else,
207 the value that is the current context is interpolated as a whole. This
208 is especially useful if the parent context is a list; see <strong>Sections</strong>
209 below.</p>
210
211 <p>Template:</p>
212
213 <pre><code>* {{.}}
214 </code></pre>
215
216 <p>Current context:</p>
217
218 <pre><code>"Hello!"
219 </code></pre>
220
221 <p>Output:</p>
222
223 <pre><code>* Hello!
224 </code></pre>
225
226 <p><strong>Lambdas</strong></p>
227
228 <p>If any value found during the lookup is a callable object, such as a
229 function or lambda, this object will be invoked with zero arguments. The
230 value that is returned is then used instead of the callable object itself.</p>
231
232 <p>An <strong>optional</strong> part of the specification states that if the final key in
233 the <code>name</code> is a lambda that returns a string, then that string should be
234 rendered as a Mustache template before interpolation. It will be rendered
235 using the default delimiters (see <strong>Set Delimiter</strong> below) against the
236 current context.</p>
237
238 <p>Template:</p>
239
240 <pre><code>* {{time.hour}}
241 * {{today}}
242 </code></pre>
243
244 <p>Hash:</p>
245
246 <pre><code>{
247 "year": 1970,
248 "month": 1,
249 "day": 1,
250 "time": function() {
251 return {
252 "hour": 0,
253 "minute": 0,
254 "second": 0
255 }
256 },
257 "today": function() {
258 return "{{year}}-{{month}}-{{day}}"
259 }
260 }
261 </code></pre>
262
263 <p>Output:</p>
264
265 <pre><code>* 0
266 * 1970-1-1
267 </code></pre>
268
269 <h3 id="Sections">Sections</h3>
270
271 <p>Sections render blocks of text zero or more times, depending on the
272 value of the key in the current context.</p>
273
274 <p>Lookup of dotted names works in the same way as with variables, except for
275 slightly different treatment of lambdas. More on this below.</p>
276
277 <p>A section begins with a pound and ends with a slash. That is,
278 <code>{{#person}}</code> begins a "person" section while <code>{{/person}}</code> ends it.</p>
279
280 <p>The behavior of the section is determined by the final value of the key
281 lookup.</p>
282
283 <p><strong>False Values or Empty Lists</strong></p>
284
285 <p>If the <code>person</code> key exists and has a value of false or an empty
286 list, the HTML between the pound and slash will not be displayed.</p>
287
288 <p>Template:</p>
289
290 <pre><code>Shown.
291 {{#person}}
292 Never shown!
293 {{/person}}
294 </code></pre>
295
296 <p>Hash:</p>
297
298 <pre><code>{
299 "person": false
300 }
301 </code></pre>
302
303 <p>Output:</p>
304
305 <pre><code>Shown.
306 </code></pre>
307
308 <p><strong>Non-Empty Lists</strong></p>
309
310 <p>If the <code>person</code> key exists and has a non-false value, the HTML between
311 the pound and slash will be rendered and displayed one or more times.</p>
312
313 <p>When the value is a non-empty list, the text in the block will be
314 displayed once for each item in the list. The context of the block
315 will be set to the current item for each iteration. In this way we can
316 loop over collections.</p>
317
318 <p>Template:</p>
319
320 <pre><code>{{#repo}}
321 &lt;b&gt;{{name}}&lt;/b&gt;
322 {{/repo}}
323 </code></pre>
324
325 <p>Hash:</p>
326
327 <pre><code>{
328 "repo": [
329 { "name": "resque" },
330 { "name": "hub" },
331 { "name": "rip" }
332 ]
333 }
334 </code></pre>
335
336 <p>Output:</p>
337
338 <pre><code> &lt;b&gt;resque&lt;/b&gt;
339 &lt;b&gt;hub&lt;/b&gt;
340 &lt;b&gt;rip&lt;/b&gt;
341 </code></pre>
342
343 <p>The same effect as above can be obtained without nested objects, by using
344 the implicit iterator (see <strong>Variables</strong> above).</p>
345
346 <p>Template:</p>
347
348 <pre><code>{{#repo}}
349 &lt;b&gt;{{.}}&lt;/b&gt;
350 {{/repo}}
351 </code></pre>
352
353 <p>Hash:</p>
354
355 <pre><code>{
356 "repo": ["resque", "hub", "rip"]
357 }
358 </code></pre>
359
360 <p>Output:</p>
361
362 <pre><code> &lt;b&gt;resque&lt;/b&gt;
363 &lt;b&gt;hub&lt;/b&gt;
364 &lt;b&gt;rip&lt;/b&gt;
365 </code></pre>
366
367 <p><strong>Lambdas</strong></p>
368
369 <p>When any value found during the lookup is a callable object, such as a
370 function or lambda, the object will be invoked and passed the block of
371 text. The text passed is the literal block, unrendered. <code>{{tags}}</code> will
372 not have been expanded.</p>
373
374 <p>An <strong>optional</strong> part of the specification states that if the final key in
375 the <code>name</code> is a lambda that returns a string, then that string replaces
376 the content of the section. It will be rendered using the same delimiters
377 (see <strong>Set Delimiter</strong> below) as the original section content. In this way
378 you can implement filters or caching.</p>
379
380 <p>Template:</p>
381
382 <pre><code>{{#wrapped}}{{name}} is awesome.{{/wrapped}}
383 </code></pre>
384
385 <p>Hash:</p>
386
387 <pre><code>{
388 "name": "Willy",
389 "wrapped": function(text) {
390 return "&lt;b&gt;" + text + "&lt;/b&gt;"
391 }
392 }
393 </code></pre>
394
395 <p>Output:</p>
396
397 <pre><code>&lt;b&gt;Willy is awesome.&lt;/b&gt;
398 </code></pre>
399
400 <p><strong>Non-False Values</strong></p>
401
402 <p>When the value is non-false but not a list, it will be used as the
403 context for a single rendering of the block.</p>
404
405 <p>Template:</p>
406
407 <pre><code>{{#person?}}
408 Hi {{name}}!
409 {{/person?}}
410 </code></pre>
411
412 <p>Hash:</p>
413
414 <pre><code>{
415 "person?": { "name": "Jon" }
416 }
417 </code></pre>
418
419 <p>Output:</p>
420
421 <pre><code> Hi Jon!
422 </code></pre>
423
424 <h3 id="Inverted-Sections">Inverted Sections</h3>
425
426 <p>An inverted section begins with a caret (hat) and ends with a
427 slash. That is <code>{{^person}}</code> begins a "person" inverted section while
428 <code>{{/person}}</code> ends it.</p>
429
430 <p>While sections can be used to render text zero or more times based on the
431 value of the key, inverted sections may render text once based
432 on the inverse value of the key. That is, they will be rendered
433 if the key doesn't exist, is false, or is an empty list.</p>
434
435 <p>Template:</p>
436
437 <pre><code>{{#repo}}
438 &lt;b&gt;{{name}}&lt;/b&gt;
439 {{/repo}}
440 {{^repo}}
441 No repos :(
442 {{/repo}}
443 </code></pre>
444
445 <p>Hash:</p>
446
447 <pre><code>{
448 "repo": []
449 }
450 </code></pre>
451
452 <p>Output:</p>
453
454 <pre><code> No repos :(
455 </code></pre>
456
457 <h3 id="Comments">Comments</h3>
458
459 <p>Comments begin with a bang and are ignored. The following template:</p>
460
461 <pre><code>&lt;h1&gt;Today{{! ignore me }}.&lt;/h1&gt;
462 </code></pre>
463
464 <p>Will render as follows:</p>
465
466 <pre><code>&lt;h1&gt;Today.&lt;/h1&gt;
467 </code></pre>
468
469 <p>Comments may contain newlines.</p>
470
471 <h3 id="Partials">Partials</h3>
472
473 <p>Partials begin with a greater than sign, like <code>{{&gt; box}}</code>.</p>
474
475 <p>Partials are rendered at runtime (as opposed to compile time), so
476 recursive partials are possible. Just avoid infinite loops.</p>
477
478 <p>They also inherit the calling context. Whereas in ERB you may have
479 this:</p>
480
481 <pre><code>&lt;%= partial :next_more, :start =&gt; start, :size =&gt; size %&gt;
482 </code></pre>
483
484 <p>Mustache requires only this:</p>
485
486 <pre><code>{{&gt; next_more}}
487 </code></pre>
488
489 <p>Why? Because the <code>next_more.mustache</code> file will inherit the <code>size</code> and
490 <code>start</code> methods from the calling context.</p>
491
492 <p>In this way you may want to think of partials as includes, or template
493 expansion, even though it's not literally true.</p>
494
495 <p>For example, this template and partial:</p>
496
497 <pre><code>base.mustache:
498 &lt;h2&gt;Names&lt;/h2&gt;
499 {{#names}}
500 {{&gt; user}}
501 {{/names}}
502
503 user.mustache:
504 &lt;strong&gt;{{name}}&lt;/strong&gt;
505 </code></pre>
506
507 <p>Can be thought of as a single, expanded template:</p>
508
509 <pre><code>&lt;h2&gt;Names&lt;/h2&gt;
510 {{#names}}
511 &lt;strong&gt;{{name}}&lt;/strong&gt;
512 {{/names}}
513 </code></pre>
514
515 <p><strong>Dynamic Names</strong></p>
516
517 <p>Partials can be loaded dynamically at runtime using Dynamic Names; an
518 <strong>optional</strong> part of the Mustache specification which allows to dynamically
519 determine a tag's content at runtime.</p>
520
521 <p>Dynamic Names consists of an asterisk, followed by a dotted name which follows
522 the same notation and the same resolution as in an variable tag. That is
523 <code>{{&gt;*dynamic}}</code>. It can be thought as the following <strong>hypothetical</strong> tag
524 (which is <strong>not allowed</strong>!): <code>{{&gt;{{dynamic}}}}</code>.</p>
525
526 <p>Templates:</p>
527
528 <pre><code>main.mustache:
529 Hello {{&gt;*dynamic}}
530
531 world.template:
532 everyone!
533 </code></pre>
534
535 <p>Hash:</p>
536
537 <pre><code>{
538 "dynamic": "world"
539 }
540 </code></pre>
541
542 <p>Output:</p>
543
544 <pre><code>Hello everyone!
545 </code></pre>
546
547 <h3 id="Blocks">Blocks</h3>
548
549 <p>A block begins with a dollar and ends with a slash. That is, <code>{{$title}}</code>
550 begins a "title" block and <code>{{/title}}</code> ends it.</p>
551
552 <p>Blocks mark parts of the template that may be overridden. This can be done
553 with a block of the same name within a parent section in the calling
554 template (see <strong>Parents</strong> below). If not overridden, the contents of a
555 block render just as if the <code>{{$title}}</code> and <code>{{/title}}</code> tags weren't
556 there.</p>
557
558 <p>Blocks could be thought of as template parameters or as inline partials
559 that may be passed to another template. They are part of the optional
560 inheritance extension.</p>
561
562 <p>Template <code>article.mustache</code>:</p>
563
564 <pre><code>&lt;h1&gt;{{$title}}The News of Today{{/title}}&lt;/h1&gt;
565 {{$body}}
566 &lt;p&gt;Nothing special happened.&lt;/p&gt;
567 {{/body}}
568 </code></pre>
569
570 <p>Output:</p>
571
572 <pre><code>&lt;h1&gt;The News of Today&lt;/h1&gt;
573 &lt;p&gt;Nothing special happened.&lt;/p&gt;
574 </code></pre>
575
576 <h3 id="Parents">Parents</h3>
577
578 <p>A parent begins with a less than sign and ends with a slash. That is,
579 <code>{{&lt;article}}</code> begins an "article" parent and <code>{{/article}}</code> ends it.</p>
580
581 <p>Like an <code>{{&gt;article}}</code> partial, a parent lets you expand another template
582 inside the current one. Unlike a partial, a parent also lets you override
583 blocks of the other template.</p>
584
585 <p>Blocks within a parent can again be overridden by another including
586 template. Other content within a parent is ignored, like comments.</p>
587
588 <p>Template:</p>
589
590 <pre><code>{{&lt;article}}
591 Never shown
592 {{$body}}
593 {{#headlines}}
594 &lt;p&gt;{{.}}&lt;/p&gt;
595 {{/headlines}}
596 {{/body}}
597 {{/article}}
598
599 {{&lt;article}}
600 {{$title}}Yesterday{{/title}}
601 {{/article}}
602 </code></pre>
603
604 <p>Hash:</p>
605
606 <pre><code>{
607 "headlines": [
608 "A pug's handler grew mustaches.",
609 "What an exciting day!"
610 ]
611 }
612 </code></pre>
613
614 <p>Output, assuming the <code>article.mustache</code> from before:</p>
615
616 <pre><code>&lt;h1&gt;The News of Today&lt;/h1&gt;
617 &lt;p&gt;A pug's handler grew mustaches.&lt;/p&gt;
618 &lt;p&gt;What an exciting day!&lt;/p&gt;
619
620 &lt;h1&gt;Yesterday&lt;/h1&gt;
621 &lt;p&gt;Nothing special happened.&lt;/p&gt;
622 </code></pre>
623
624 <p><strong>Dynamic Names</strong></p>
625
626 <p>Some mustache implementations may allow the use of Dynamic Names in
627 parent tags, similar to dynamic names in partials. Here's an example of
628 how Dynamic Names in parent tags work.</p>
629
630 <p>Templates:</p>
631
632 <pre><code>{{!normal.mustache}}
633 {{$text}}Here goes nothing.{{/text}}
634
635 {{!bold.mustache}}
636 &lt;b&gt;{{$text}}Here also goes nothing but it's bold.{{/text}}&lt;/b&gt;
637
638 {{!dynamic.mustache}}
639 {{&lt;*dynamic}}
640 {{$text}}Hello World!{{/text}}
641 {{/*dynamic}}
642 </code></pre>
643
644 <p>Hash:</p>
645
646 <pre><code>{
647 "dynamic": "bold"
648 }
649 </code></pre>
650
651 <p>Output:</p>
652
653 <pre><code>&lt;b&gt;Hello World!&lt;/b&gt;
654 </code></pre>
655
656 <h3 id="Set-Delimiter">Set Delimiter</h3>
657
658 <p>Set Delimiter tags start with an equal sign and change the tag
659 delimiters from <code>{{</code> and <code>}}</code> to custom strings.</p>
660
661 <p>Consider the following contrived example:</p>
662
663 <pre><code>* {{default_tags}}
664 {{=&lt;% %&gt;=}}
665 * &lt;% erb_style_tags %&gt;
666 &lt;%={{ }}=%&gt;
667 * {{ default_tags_again }}
668 </code></pre>
669
670 <p>Here we have a list with three items. The first item uses the default
671 tag style, the second uses erb style as defined by the Set Delimiter
672 tag, and the third returns to the default style after yet another Set
673 Delimiter declaration.</p>
674
675 <p>According to <a href="http://goog-ctemplate.sourceforge.net/doc/howto.html">ctemplates</a>, this "is useful for languages like TeX, where
676 double-braces may occur in the text and are awkward to use for
677 markup."</p>
678
679 <p>Custom delimiters may not contain whitespace or the equals sign.</p>
680
681 <h2 id="COPYRIGHT">COPYRIGHT</h2>
682
683 <p>Mustache is Copyright (C) 2009 Chris Wanstrath</p>
684
685 <p>Original CTemplate by Google</p>
686
687 <h2 id="SEE-ALSO">SEE ALSO</h2>
688
689 <p><a class="man-ref" href="mustache.1.ron.html">mustache<span class="s">(1)</span></a>,
690 <a href="http://mustache.github.io/" data-bare-link="true">http://mustache.github.io/</a></p>
691
692 <ol class='man-decor man-foot man foot'>
693 <li class='tl'>DEFUNKT</li>
694 <li class='tc'>September 2022</li>
695 <li class='tr'>mustache(5)</li>
696 </ol>
697
698 </div>
699 </body>
700 </html>