Debian Codegraph @

man/mustache.5

raw
1 .\" generated with Ronn-NG/v0.9.1
2 .\" http://github.com/apjanke/ronn-ng/tree/0.9.1
3 .TH "MUSTACHE" "5" "September 2022" "DEFUNKT" "Mustache Manual"
4 .SH "NAME"
5 \fBmustache\fR \- Logic\-less templates\.
6 .SH "SYNOPSIS"
7 A typical Mustache template:
8 .IP "" 4
9 .nf
10 Hello {{name}}
11 You have just won {{value}} dollars!
12 {{#in_ca}}
13 Well, {{taxed_value}} dollars, after taxes\.
14 {{/in_ca}}
15 .fi
16 .IP "" 0
17 .P
18 Given the following hash:
19 .IP "" 4
20 .nf
21 {
22 "name": "Chris",
23 "value": 10000,
24 "taxed_value": 10000 \- (10000 * 0\.4),
25 "in_ca": true
26 }
27 .fi
28 .IP "" 0
29 .P
30 Will produce the following:
31 .IP "" 4
32 .nf
33 Hello Chris
34 You have just won 10000 dollars!
35 Well, 6000\.0 dollars, after taxes\.
36 .fi
37 .IP "" 0
38 .SH "DESCRIPTION"
39 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\.
40 .P
41 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\.
42 .P
43 The Mustache language has a formal specification \fIhttps://github\.com/mustache/spec\fR\. The current manpage reflects version 1\.3\.0 of the specification, including the official\-but\-optional extensions for lambdas and inheritance\.
44 .SH "TAG TYPES"
45 Tags are indicated by the double mustaches\. \fB{{person}}\fR is a tag, as is \fB{{#person}}\fR\. In both examples, we'd refer to \fBperson\fR as the key or tag key\. Let's talk about the different types of tags\.
46 .SS "Variables"
47 The most basic tag type is the variable\. A \fB{{name}}\fR tag in a basic template will try to find the \fBname\fR key in the current context\. If there is no \fBname\fR key, the parent contexts will be checked recursively\. If the top context is reached and the \fBname\fR key is still not found, nothing will be rendered\.
48 .P
49 All variables are HTML escaped by default\. If you want to return raw contents without escaping, use the triple mustache: \fB{{{name}}}\fR\.
50 .P
51 You can also use \fB&\fR to return its raw contents: \fB{{& name}}\fR\. This may be useful when changing delimiters (see "Set Delimiter" below)\.
52 .P
53 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\.
54 .P
55 Template:
56 .IP "" 4
57 .nf
58 * {{name}}
59 * {{age}}
60 * {{company}}
61 * {{{company}}}
62 .fi
63 .IP "" 0
64 .P
65 Hash:
66 .IP "" 4
67 .nf
68 {
69 "name": "Chris",
70 "company": "<b>GitHub</b>"
71 }
72 .fi
73 .IP "" 0
74 .P
75 Output:
76 .IP "" 4
77 .nf
78 * Chris
79 *
80 * &lt;b&gt;GitHub&lt;/b&gt;
81 * <b>GitHub</b>
82 .fi
83 .IP "" 0
84 .P
85 \fBDotted Names\fR
86 .P
87 If the \fBname\fR contains dots, it is split on the dots to obtain multiple keys\. The first key is looked up in the context as described above\. If it is found, the next key is looked up within the previous result\. This is repeated until a key is not found or until the last key is found\. The final result is interpolated as above\.
88 .P
89 Template:
90 .IP "" 4
91 .nf
92 * {{client\.name}}
93 * {{age}}
94 * {{client\.company\.name}}
95 * {{{company\.name}}}
96 .fi
97 .IP "" 0
98 .P
99 Hash:
100 .IP "" 4
101 .nf
102 {
103 "client": {
104 "name": "Chris & Friends",
105 "age": 50
106 },
107 "company": {
108 "name": "<b>GitHub</b>"
109 }
110 }
111 .fi
112 .IP "" 0
113 .P
114 Output:
115 .IP "" 4
116 .nf
117 * Chris &amp; Friends
118 *
119 *
120 * <b>GitHub</b>
121 .fi
122 .IP "" 0
123 .P
124 \fBImplicit Iterator\fR
125 .P
126 As a special case, if the \fBname\fR consists of only a dot and nothing else, the value that is the current context is interpolated as a whole\. This is especially useful if the parent context is a list; see \fBSections\fR below\.
127 .P
128 Template:
129 .IP "" 4
130 .nf
131 * {{\.}}
132 .fi
133 .IP "" 0
134 .P
135 Current context:
136 .IP "" 4
137 .nf
138 "Hello!"
139 .fi
140 .IP "" 0
141 .P
142 Output:
143 .IP "" 4
144 .nf
145 * Hello!
146 .fi
147 .IP "" 0
148 .P
149 \fBLambdas\fR
150 .P
151 If any value found during the lookup is a callable object, such as a function or lambda, this object will be invoked with zero arguments\. The value that is returned is then used instead of the callable object itself\.
152 .P
153 An \fBoptional\fR part of the specification states that if the final key in the \fBname\fR is a lambda that returns a string, then that string should be rendered as a Mustache template before interpolation\. It will be rendered using the default delimiters (see \fBSet Delimiter\fR below) against the current context\.
154 .P
155 Template:
156 .IP "" 4
157 .nf
158 * {{time\.hour}}
159 * {{today}}
160 .fi
161 .IP "" 0
162 .P
163 Hash:
164 .IP "" 4
165 .nf
166 {
167 "year": 1970,
168 "month": 1,
169 "day": 1,
170 "time": function() {
171 return {
172 "hour": 0,
173 "minute": 0,
174 "second": 0
175 }
176 },
177 "today": function() {
178 return "{{year}}\-{{month}}\-{{day}}"
179 }
180 }
181 .fi
182 .IP "" 0
183 .P
184 Output:
185 .IP "" 4
186 .nf
187 * 0
188 * 1970\-1\-1
189 .fi
190 .IP "" 0
191 .SS "Sections"
192 Sections render blocks of text zero or more times, depending on the value of the key in the current context\.
193 .P
194 Lookup of dotted names works in the same way as with variables, except for slightly different treatment of lambdas\. More on this below\.
195 .P
196 A section begins with a pound and ends with a slash\. That is, \fB{{#person}}\fR begins a "person" section while \fB{{/person}}\fR ends it\.
197 .P
198 The behavior of the section is determined by the final value of the key lookup\.
199 .P
200 \fBFalse Values or Empty Lists\fR
201 .P
202 If the \fBperson\fR key exists and has a value of false or an empty list, the HTML between the pound and slash will not be displayed\.
203 .P
204 Template:
205 .IP "" 4
206 .nf
207 Shown\.
208 {{#person}}
209 Never shown!
210 {{/person}}
211 .fi
212 .IP "" 0
213 .P
214 Hash:
215 .IP "" 4
216 .nf
217 {
218 "person": false
219 }
220 .fi
221 .IP "" 0
222 .P
223 Output:
224 .IP "" 4
225 .nf
226 Shown\.
227 .fi
228 .IP "" 0
229 .P
230 \fBNon\-Empty Lists\fR
231 .P
232 If the \fBperson\fR key exists and has a non\-false value, the HTML between the pound and slash will be rendered and displayed one or more times\.
233 .P
234 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\.
235 .P
236 Template:
237 .IP "" 4
238 .nf
239 {{#repo}}
240 <b>{{name}}</b>
241 {{/repo}}
242 .fi
243 .IP "" 0
244 .P
245 Hash:
246 .IP "" 4
247 .nf
248 {
249 "repo": [
250 { "name": "resque" },
251 { "name": "hub" },
252 { "name": "rip" }
253 ]
254 }
255 .fi
256 .IP "" 0
257 .P
258 Output:
259 .IP "" 4
260 .nf
261 <b>resque</b>
262 <b>hub</b>
263 <b>rip</b>
264 .fi
265 .IP "" 0
266 .P
267 The same effect as above can be obtained without nested objects, by using the implicit iterator (see \fBVariables\fR above)\.
268 .P
269 Template:
270 .IP "" 4
271 .nf
272 {{#repo}}
273 <b>{{\.}}</b>
274 {{/repo}}
275 .fi
276 .IP "" 0
277 .P
278 Hash:
279 .IP "" 4
280 .nf
281 {
282 "repo": ["resque", "hub", "rip"]
283 }
284 .fi
285 .IP "" 0
286 .P
287 Output:
288 .IP "" 4
289 .nf
290 <b>resque</b>
291 <b>hub</b>
292 <b>rip</b>
293 .fi
294 .IP "" 0
295 .P
296 \fBLambdas\fR
297 .P
298 When any value found during the lookup 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\. \fB{{tags}}\fR will not have been expanded\.
299 .P
300 An \fBoptional\fR part of the specification states that if the final key in the \fBname\fR is a lambda that returns a string, then that string replaces the content of the section\. It will be rendered using the same delimiters (see \fBSet Delimiter\fR below) as the original section content\. In this way you can implement filters or caching\.
301 .P
302 Template:
303 .IP "" 4
304 .nf
305 {{#wrapped}}{{name}} is awesome\.{{/wrapped}}
306 .fi
307 .IP "" 0
308 .P
309 Hash:
310 .IP "" 4
311 .nf
312 {
313 "name": "Willy",
314 "wrapped": function(text) {
315 return "<b>" + text + "</b>"
316 }
317 }
318 .fi
319 .IP "" 0
320 .P
321 Output:
322 .IP "" 4
323 .nf
324 <b>Willy is awesome\.</b>
325 .fi
326 .IP "" 0
327 .P
328 \fBNon\-False Values\fR
329 .P
330 When the value is non\-false but not a list, it will be used as the context for a single rendering of the block\.
331 .P
332 Template:
333 .IP "" 4
334 .nf
335 {{#person?}}
336 Hi {{name}}!
337 {{/person?}}
338 .fi
339 .IP "" 0
340 .P
341 Hash:
342 .IP "" 4
343 .nf
344 {
345 "person?": { "name": "Jon" }
346 }
347 .fi
348 .IP "" 0
349 .P
350 Output:
351 .IP "" 4
352 .nf
353 Hi Jon!
354 .fi
355 .IP "" 0
356 .SS "Inverted Sections"
357 An inverted section begins with a caret (hat) and ends with a slash\. That is \fB{{^person}}\fR begins a "person" inverted section while \fB{{/person}}\fR ends it\.
358 .P
359 While sections can be used to render text zero 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\.
360 .P
361 Template:
362 .IP "" 4
363 .nf
364 {{#repo}}
365 <b>{{name}}</b>
366 {{/repo}}
367 {{^repo}}
368 No repos :(
369 {{/repo}}
370 .fi
371 .IP "" 0
372 .P
373 Hash:
374 .IP "" 4
375 .nf
376 {
377 "repo": []
378 }
379 .fi
380 .IP "" 0
381 .P
382 Output:
383 .IP "" 4
384 .nf
385 No repos :(
386 .fi
387 .IP "" 0
388 .SS "Comments"
389 Comments begin with a bang and are ignored\. The following template:
390 .IP "" 4
391 .nf
392 <h1>Today{{! ignore me }}\.</h1>
393 .fi
394 .IP "" 0
395 .P
396 Will render as follows:
397 .IP "" 4
398 .nf
399 <h1>Today\.</h1>
400 .fi
401 .IP "" 0
402 .P
403 Comments may contain newlines\.
404 .SS "Partials"
405 Partials begin with a greater than sign, like \fB{{> box}}\fR\.
406 .P
407 Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible\. Just avoid infinite loops\.
408 .P
409 They also inherit the calling context\. Whereas in ERB you may have this:
410 .IP "" 4
411 .nf
412 <%= partial :next_more, :start => start, :size => size %>
413 .fi
414 .IP "" 0
415 .P
416 Mustache requires only this:
417 .IP "" 4
418 .nf
419 {{> next_more}}
420 .fi
421 .IP "" 0
422 .P
423 Why? Because the \fBnext_more\.mustache\fR file will inherit the \fBsize\fR and \fBstart\fR methods from the calling context\.
424 .P
425 In this way you may want to think of partials as includes, or template expansion, even though it's not literally true\.
426 .P
427 For example, this template and partial:
428 .IP "" 4
429 .nf
430 base\.mustache:
431 <h2>Names</h2>
432 {{#names}}
433 {{> user}}
434 {{/names}}
435
436 user\.mustache:
437 <strong>{{name}}</strong>
438 .fi
439 .IP "" 0
440 .P
441 Can be thought of as a single, expanded template:
442 .IP "" 4
443 .nf
444 <h2>Names</h2>
445 {{#names}}
446 <strong>{{name}}</strong>
447 {{/names}}
448 .fi
449 .IP "" 0
450 .P
451 \fBDynamic Names\fR
452 .P
453 Partials can be loaded dynamically at runtime using Dynamic Names; an \fBoptional\fR part of the Mustache specification which allows to dynamically determine a tag's content at runtime\.
454 .P
455 Dynamic Names consists of an asterisk, followed by a dotted name which follows the same notation and the same resolution as in an variable tag\. That is \fB{{>*dynamic}}\fR\. It can be thought as the following \fBhypothetical\fR tag (which is \fBnot allowed\fR!): \fB{{>{{dynamic}}}}\fR\.
456 .P
457 Templates:
458 .IP "" 4
459 .nf
460 main\.mustache:
461 Hello {{>*dynamic}}
462
463 world\.template:
464 everyone!
465 .fi
466 .IP "" 0
467 .P
468 Hash:
469 .IP "" 4
470 .nf
471 {
472 "dynamic": "world"
473 }
474 .fi
475 .IP "" 0
476 .P
477 Output:
478 .IP "" 4
479 .nf
480 Hello everyone!
481 .fi
482 .IP "" 0
483 .SS "Blocks"
484 A block begins with a dollar and ends with a slash\. That is, \fB{{$title}}\fR begins a "title" block and \fB{{/title}}\fR ends it\.
485 .P
486 Blocks mark parts of the template that may be overridden\. This can be done with a block of the same name within a parent section in the calling template (see \fBParents\fR below)\. If not overridden, the contents of a block render just as if the \fB{{$title}}\fR and \fB{{/title}}\fR tags weren't there\.
487 .P
488 Blocks could be thought of as template parameters or as inline partials that may be passed to another template\. They are part of the optional inheritance extension\.
489 .P
490 Template \fBarticle\.mustache\fR:
491 .IP "" 4
492 .nf
493 <h1>{{$title}}The News of Today{{/title}}</h1>
494 {{$body}}
495 <p>Nothing special happened\.</p>
496 {{/body}}
497 .fi
498 .IP "" 0
499 .P
500 Output:
501 .IP "" 4
502 .nf
503 <h1>The News of Today</h1>
504 <p>Nothing special happened\.</p>
505 .fi
506 .IP "" 0
507 .SS "Parents"
508 A parent begins with a less than sign and ends with a slash\. That is, \fB{{<article}}\fR begins an "article" parent and \fB{{/article}}\fR ends it\.
509 .P
510 Like an \fB{{>article}}\fR partial, a parent lets you expand another template inside the current one\. Unlike a partial, a parent also lets you override blocks of the other template\.
511 .P
512 Blocks within a parent can again be overridden by another including template\. Other content within a parent is ignored, like comments\.
513 .P
514 Template:
515 .IP "" 4
516 .nf
517 {{<article}}
518 Never shown
519 {{$body}}
520 {{#headlines}}
521 <p>{{\.}}</p>
522 {{/headlines}}
523 {{/body}}
524 {{/article}}
525
526 {{<article}}
527 {{$title}}Yesterday{{/title}}
528 {{/article}}
529 .fi
530 .IP "" 0
531 .P
532 Hash:
533 .IP "" 4
534 .nf
535 {
536 "headlines": [
537 "A pug's handler grew mustaches\.",
538 "What an exciting day!"
539 ]
540 }
541 .fi
542 .IP "" 0
543 .P
544 Output, assuming the \fBarticle\.mustache\fR from before:
545 .IP "" 4
546 .nf
547 <h1>The News of Today</h1>
548 <p>A pug's handler grew mustaches\.</p>
549 <p>What an exciting day!</p>
550
551 <h1>Yesterday</h1>
552 <p>Nothing special happened\.</p>
553 .fi
554 .IP "" 0
555 .P
556 \fBDynamic Names\fR
557 .P
558 Some mustache implementations may allow the use of Dynamic Names in parent tags, similar to dynamic names in partials\. Here's an example of how Dynamic Names in parent tags work\.
559 .P
560 Templates:
561 .IP "" 4
562 .nf
563 {{!normal\.mustache}}
564 {{$text}}Here goes nothing\.{{/text}}
565
566 {{!bold\.mustache}}
567 <b>{{$text}}Here also goes nothing but it's bold\.{{/text}}</b>
568
569 {{!dynamic\.mustache}}
570 {{<*dynamic}}
571 {{$text}}Hello World!{{/text}}
572 {{/*dynamic}}
573 .fi
574 .IP "" 0
575 .P
576 Hash:
577 .IP "" 4
578 .nf
579 {
580 "dynamic": "bold"
581 }
582 .fi
583 .IP "" 0
584 .P
585 Output:
586 .IP "" 4
587 .nf
588 <b>Hello World!</b>
589 .fi
590 .IP "" 0
591 .SS "Set Delimiter"
592 Set Delimiter tags start with an equal sign and change the tag delimiters from \fB{{\fR and \fB}}\fR to custom strings\.
593 .P
594 Consider the following contrived example:
595 .IP "" 4
596 .nf
597 * {{default_tags}}
598 {{=<% %>=}}
599 * <% erb_style_tags %>
600 <%={{ }}=%>
601 * {{ default_tags_again }}
602 .fi
603 .IP "" 0
604 .P
605 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\.
606 .P
607 According to ctemplates \fIhttp://goog\-ctemplate\.sourceforge\.net/doc/howto\.html\fR, this "is useful for languages like TeX, where double\-braces may occur in the text and are awkward to use for markup\."
608 .P
609 Custom delimiters may not contain whitespace or the equals sign\.
610 .SH "COPYRIGHT"
611 Mustache is Copyright (C) 2009 Chris Wanstrath
612 .P
613 Original CTemplate by Google
614 .SH "SEE ALSO"
615 mustache(1), \fIhttp://mustache\.github\.io/\fR