Debian Codegraph @

man/mustache.5.ron

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