
1. 典型模板
模板demo1.mustache
Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}
数据demo1.yml
---
"name": "Chris"
"value": 10000
"taxed_value": 10000 - (10000 * 0.4)
"in_ca": true
---
命令
/var/lib/gems/1.8/bin/mustache demo1.yml demo1.mustache
输出
Hello Chris
You have just won $10000!
Well, $10000-(10000*0.4), after taxes.
注意yml中的格式,:后面有空格
2. 转义和非转义
模板demo2.mustache
* {{name}}
* {{age}}
* {{company}}
* {{{company}}}
数据demo2.yml
---
"name": "Chris"
"company": "<b>GitHub</b>"
---
命令:
/var/lib/gems/1.8/bin/mustache demo2.yml demo2.mustache
输出:
* Chris
*
* <b>GitHub</b>
* <b>GitHub</b>
3.小节
模板demo3.mustache
Shown.
{{#nothin}}
Never shown!
{{/nothin}}
数据demo3.yml
---
"person": true
---
命令,还可以用下面方式:
cat demo3.yml demo3.mustache | /var/lib/gems/1.8/bin/mustache
输出:
Shown.
4. 非空列表
模板demo4.mustache
{{#repo}}
<b>{{name}}</b>
{{/repo}}
数据
---
"repo":
- "name": "resque"
- "name": "hub"
- "name": "rip"
---
命令
/var/lib/gems/1.8/bin/mustache demo4.yml demo4.mustache
输出
<b>resque</b>
<b>hub</b>
<b>rip</b>
5. 匿名函数
模板demo5.mustache
{{#wrapped}}
{{name}} is awesome.
{{/wrapped}}
数据demo5.yml
---
"name": "Willy"
"wrapped": function() {
return function(text) {
return "<b>" + render(text) + "</b>"
}
}
---
命令
/var/lib/gems/1.8/bin/mustache demo5.yml demo5.mustache
输出
Willy is awesome.
6. 非否定值
模板demo6.mustache
{{#person?}}
Hi {{name}}!
{{/person?}}
数据 demo6.yml
---
"person?":
- "name": "Jon"
---
命令
/var/lib/gems/1.8/bin/mustache demo6.yml demo6.mustache
输出
Hi Jon!
7. 反转小节
模板demo7.mustache
{{#repo}}
<b>{{name}}</b>
{{/repo}}
{{^repo}}
No repos
{{/repo}}
数据demo7.yml
---
"repo": []
---
命令
/var/lib/gems/1.8/bin/mustache demo7.yml demo7.mustache
输出
No repos
8. 注释
模板demo8.mustache
<h1>Today{{! ignore me }}.</h1>
命令
/var/lib/gems/1.8/bin/mustache demo8.mustache
输出
<h1>Today.</h1>
上面模板就是写成下面这样也是相同的输出:
<h1>Today{{! ignore me
This is a comment test!!!
Test row!!!!
}}.</h1>
9. 局部模板
模板base.mustache
<h2>Names</h2>
{{#repo}}
{{> user}}
{{/repo}}
局部模板user.mustache
<strong>{{name}}</strong>
数据用demo4.yml
命令
/var/lib/gems/1.8/bin/mustache demo4.yml base.mustache
输出
<h2>Names</h2>
<strong>resque</strong>
<strong>hub</strong>
<strong>rip</strong>
10. 设置分隔符
模板setdelimiter.mustache
* {{default_tags}}
{{=<% %>=}}
{{name}}
<%name%>
* <% erb_style_tags %>
<%={{ }}=%>
<%name%>
{{name}}
* {{ default_tags_again }}
命令
/var/lib/gems/1.8/bin/mustache setdelimiter.mustache
输出
*
{{name}}
*
<%name%>
*
11.