mustache(5)例子测试

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
*
* &lt;b&gt;GitHub&lt;/b&gt;
* <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.

mustache — Logic-less templates

MUSTACHE(5)MUSTACHE MANUALMUSTACHE(5)
NAME/名称
mustache — Logic-less templates./少逻辑模板。

SYNOPSIS/摘要
A typical Mustache template:/一个典型的mustache模板:

Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}
Given the following hash:/给定下面的hash数据:

{
“name”: “Chris”,
“value”: 10000,
“taxed_value”: 10000 – (10000 * 0.4),
“in_ca”: true
}

Will produce the following:/将会产生下面的结果:

Hello Chris
You have just won $10000!
Well, $6000.0, after taxes.

DESCRIPTION/描述
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./Mustache可以用来产生HTML,配置文件,源码或者任何东西。它通过使用hash或者对象中的数据扩展模板中的标签来工作。

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./我们称其为“无逻辑”,因为它没有if语句,else子句或者for循环。除此之外,仅有标签。一些标签会被值来替换,或者被替换成空,其它情况会产生一系列值。本文档解释了Mustache标签的不同类型。

TAG TYPES/标签类型
Tags are indicated by the double mustaches. {{person}} is a tag, as is {{#person}}. In both examples, we’d refer to person as the key or tag key. Let’s talk about the different types of tags.标签由双的胡子{}组成(这东西象胡子吗?)。{{person}}是一个标签,就象{{#person}}。这两个例子中,我们会认为person是一个键值或者是标签键。让我们来谈谈标签的类型吧。

Variables/变量
The most basic tag type is the variable. A {{name}} tag in a basic template will try to find the name key in the current context. If there is no name key, nothing will be rendered./最基本的标签类型就是变量。在基本的模板中出现的{{name}}标签会试图在当前的上下文中寻找名为name的键值。如果没有这个键值,则不会有任何内容会被渲染。

All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}./所有的变量缺省地都是HTML转义的。如果要你想返回未转义的HTML内容,可以使用三联标签:{{{name}}}。

You can also use & to unescape a variable: {{& name}}. This may be useful when changing delimiters (see “Set Delimiter” below)./你也可以使用&符号来非转义一个变量,如{{&name}}.这个在改变了分隔符的时候会非常有用(见下“设置分隔符”)。

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./默认的一个变量找不到时将返回一个空字符串。这个通常会在你的Mustache库配置。例如,Ruby版本的Mustacle支持在这种情况下抛出异常。

Template:/模板

* {{name}}
* {{age}}
* {{company}}
* {{{company}}}
Hash:/数据

{
“name”: “Chris”,
“company”: “GitHub
}
Output:/输出

* Chris
*
* <b>GitHub</b>
* <strong>GitHub</strong>

Sections/小节
Sections render blocks of text one or more times, depending on the value of the key in the current context./小节会将文本块渲染一次或者多次,这个要看在当前上下文中的键的值的情况。

A section begins with a pound and ends with a slash. That is, {{#person}} begins a “person” section while {{/person}} ends it./一个小节由#号标签开始,用/号标签结束。即:{{#person}会开始一个person小节,然后用{{/person}}结束。

The behavior of the section is determined by the value of the key./小节的行为取决于指定键的值。

False Values or Empty Lists/否定值或者空的列表

If the person key exists and has a value of false or an empty list, the HTML between the pound and slash will not be displayed./如果person键存在,但是有个否定值或者是一个空的列表,则在#和/之间的HTML内容不会被显示。

Template:/模板:

Shown.
{{#nothin}}
Never shown!
{{/nothin}}
Hash:/数据

{
“person”: true,
}
Output:

Shown.
Non-Empty Lists/非空列表

If the person key exists and has a non-false value, the HTML between the pound and slash will be rendered and displayed one or more times./如果一个person键存在,并且有一个非空的值,则在#和/标签之间的内容就会被渲染和显示一次或者多次。

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./当值是非空列表中的时候,块中的文本内容会按列表中的条目每一条显示一次。块的上下文会在每一次迭代的时候被设置成当前的条目。在这种情况下我们就可以对那些值进行循环了。

Template:/模板:

{{#repo}}
{{name}}
{{/repo}}
Hash:/值:

{
“repo”: [
{ “name”: “resque” },
{ “name”: “hub” },
{ “name”: “rip” },
]
}
Output:/输出:

resque
hub
rip
Lambdas/匿名

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. {{tags}} will not have been expanded – the lambda should do that on its own. In this way you can implement filters or caching./当那个值是一个可调用的对象的时候,比如一个函数或者匿名函数,该对象就会被调用,然后对应块的文本内容就会被传入。传入原始块的文本内容不会被渲染。{{tags}}不会被扩展,在它基础上的匿名函数会被调用。这种情况下你可以实现过滤或者缓存。

Template:/模板:

{{#wrapped}}
{{name}} is awesome.
{{/wrapped}}
Hash:/值:

{
“name”: “Willy”,
“wrapped”: function() {
return function(text) {
return “” + render(text) + “
}
}
}
Output:/输出:

Willy is awesome.
Non-False Values/非否定值

When the value is non-false but not a list, it will be used as the context for a single rendering of the block./当一个变量是一个非否定值,但是不是一个列表,它会在块渲染时被当做上下文使用。

Template:/模板:

{{#person?}}
Hi {{name}}!
{{/person?}}
Hash:/值:

{
“person?”: { “name”: “Jon” }
}
Output:/输出:

Hi Jon!
Inverted Sections/反转的小节
An inverted section begins with a caret (hat) and ends with a slash. That is {{^person}} begins a “person” inverted section while {{/person}} ends it.反转的小节会以^(帽子)符号开始,以/号结束。即:{{^person}}开始了一个反转的person小节,用{{/person}}结束。

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.就象小节会根据键的值将渲染内容渲染一次或者多次,一个反转的小节会在值相反的情况下渲染文本内容一次。即,它会在键值不存在,是否的情况或者是空列表的情况下才会被渲染。

Template:/模板:

{{#repo}}
{{name}}
{{/repo}}
{{^repo}}
No repos 🙁
{{/repo}}
Hash:/值:

{
“repo”: []
}
Output:/输出

No repos 🙁
Comments/注释
Comments begin with a bang and are ignored. The following template:/注释以感叹号开始,内容会被忽略,如下面模板:

Today{{! ignore me }}.

Will render as follows:/这个模板将被渲染如下:

Today.

Comments may contain newlines./注释中可以包含换行。

Partials/局部模板
Partials begin with a greater than sign, like {{> box}}./局部模板会由一个大于号开始,象{{>box}}。

Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible. Just avoid infinite loops./局部模板会在运行时(与编译时相对)被渲染,所以递归的局部模板是可能的。仅仅需要避免无限循环。

They also inherit the calling context. Whereas in ERB you may have this:/它们也继承了调用的上下文。就象在RoR的ERB模板中你可以做的一样:

< %= partial :next_more, :start => start, :size => size %>
Mustache requires only this:/在Mustache模板中只需要:

{{> next_more}}
Why? Because the next_more.mustache file will inherit the size and start methods from the calling context./这是为什么呢?因为next_more.mustache文件会在上下文中继承size和start方法。

In this way you may want to think of partials as includes, or template expansion, even though it’s not literally true./这种情况下你可能想认为局部模板就是包含,模板扩展,不过它确切来说不是对的。

For example, this template and partial:/例如,下面的模板和局部模板:

base.mustache:

Names

{{#names}}
{{> user}}
{{/names}}

user.mustache:
{{name}}
Can be thought of as a single, expanded template:/这个user.mustache局部模板可以用来做为扩展模板的一个单独的部分,即:

Names

{{#names}}
{{name}}
{{/names}}
Set Delimiter/设置分隔符
Set Delimiter tags start with an equal sign and change the tag delimiters from {{ and }} to custom strings./设置分隔符标签会由等号标志开始,将标签分隔符从{{和}}修改成缺省的字符串。

Consider the following contrived example:/请看下面巧妙的例子:

* {{default_tags}}
{{=< % %>=}}
* < % erb_style_tags %>
< %={{ }}=%>
* {{ default_tags_again }}
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.

According to ctemplates, this “is useful for languages like TeX, where double-braces may occur in the text and are awkward to use for markup.”

Custom delimiters may not contain whitespace or the equals sign.

COPYRIGHT/版权
Mustache is Copyright (C) 2009 Chris Wanstrath

Original CTemplate by Google

SEE ALSO/参见
mustache(1), mustache(7), http://mustache.github.com/

DEFUNKTAPRIL 2010MUSTACHE(5)

http://mustache.github.com/mustache.5.html

小注:胡乱翻译,有问题请反馈。多谢。

胡子手册:mustache — Mustache processor

这是部关于“胡子”的手册。传说中应用于PHP, Java, Ruby, Python, JavaScript, erLang等牛掰事物上的模板。至于这么介于牛A和牛C之间的事务为什么叫做胡子,据某不大健康的思路大致如下:
1. 可能如果以男人为模板,胡子大概是比较重要的部分,虽然现代人的社会大部分男人的胡子都脱不了被刮除的风险。
2. 如果有了胡子,则可以更为男人一点。如果使用了mustache模板,则上通五湖,下达四海,各类前端工程师可以游刃于纯美之间,无需考虑各种开发的小小刀片之差异。
3. 大棵一点的是有了胡子,至少么还象个男人,谁还在于您阳货之不具耶?
4. 更恶的一点:胡子么,既名之曰mustache,这痛么必然是有一点点的,反正痛过了就没事了:D。
5. 胡子挡住了你的嘴,所以你说的内容胡子上是看不出来的,这样用户真不会关注你后端的实现办法了。:)
以上胡扯,如有愿意对号入座者,自行分辨,或者可以自行摸一下,是不是硬硬的还在:D。若是觉得一个mustache就可以解决您公司复杂的python/Java/RoR/PHP之类混乱开发方式引起的使用了django/grails/rhtml/smarty之类模板的混乱,窃以为切切不可。
下面开始翻译mustache模板手册:

MUSTACHE(1) MUSTACHE MANUAL

NAME/名称
mustache — Mustache processor

SYNOPSIS/摘要
mustache
mustache –compile
mustache –tokens
DESCRIPTION/描述
Mustache is a logic-less templating system for HTML, config files, anything.
Mustache是一个用于HTML/配置文件或任何一种文档的无逻辑模板系统。
The mustache command processes a Mustache template preceded by YAML frontmatter from standard input and prints one or more documents to standard output.
mustache命令会使用由标准输入预提供的YAML数据,然后打印一个或者多个文档到标准输出。
YAML frontmatter beings with — on a single line, followed by YAML, ending with another — on a single line, e.g./该YAML的输入格式由一行—开始,跟着是YAML内容,再由另一行—结束。例如

---
names: [ {name: chris}, {name: mark}, {name: scott} ]
---

If you are unfamiliar with YAML, it is a superset of JSON. Valid JSON should work fine.如果你对YAML不熟悉,它是JSON的超集。合法的JSON也可以工作得很好。

After the frontmatter should come any valid Mustache template. See mustache(5) for an overview of Mustache templates./在数据之后跟着合法的Mustache模板。参见mustache(5)来获得Mustache模板的概况。

For example/例如:

{{#names}}
Hi {{name}}!
{{/names}}

Now let’s combine them/现在让我们来整合它们.

$ cat data.yml


names: [ {name: chris}, {name: mark}, {name: scott} ]

$ cat template.mustache

{{#names}}
Hi {{name}}!
{{/names}}

$ cat data.yml template.mustache | mustache

Hi chris!
Hi mark!
Hi scott!

If you provide multiple YAML documents (as delimited by —), your template will be rendered multiple times. Like a mail merge./如果你提供多个YAML文档(用—来分隔),模板会渲染多次。就象邮件合并。

For example/例如:

$ cat data.yml


name: chris

name: mark

name: scott

$ cat template.mustache

Hi {{name}}!

$ cat data.yml template.mustache | mustache

Hi chris!
Hi mark!
Hi scott!
OPTIONS/选项
By default mustache will try to render a Mustache template using the YAML frontmatter you provide. It can do a few other things, however.缺省地mustache命令会试图用你提供的YAML格式数据来渲染一个Mustache模板。当然,它还可以做一些其它的事情。

-c, –compile
Print the compiled Ruby version of a given template. This is the code that is actually used when rendering a template into a string. Useful for debugging but only if you are familiar with Mustache’s internals.
打印给定模板的编译后Ruby版本代码。这个代码是将模板渲染成字符串时真实使用的代码。这个代码对于高度来说是非常有用的,但是仅适用于你对Mustache内部非常熟悉的情况。

-t, –tokens
Print the tokenized form of a given Mustache template. This can be used to understand how Mustache parses a template. The tokens are handed to a generator which compiles them into a Ruby string. Syntax errors and confused tags, therefor, can probably be identified by examining the tokens produced.
打印指定Mustache模板标记后样式。这个可以用来理解Mustache是如何解析一个模板的。这些标记由一个产生器来处理,这个产生器将它们编译到一个Ruby字符串中。语法错误和令人糊涂的标签,就可能通过检测标识的产生来被认知。

INSTALLATION/安装
If you have RubyGems installed/如果你已经安装了RubyGems:

gem install mustache

EXAMPLES/例子

$ mustache data.yml template.mustache
$ cat data.yml | mustache - template.mustache
$ mustache -c template.mustache
$ cat < ---
name: Bob
age: 30
---
data

COPYRIGHT/版权
Mustache is Copyright (C) 2009 Chris Wanstrath

Original CTemplate by Google(源自Google CTemplate)

SEE ALSO/参见
mustache(5), mustache(7), gem(1), http://mustache.github.com/

http://mustache.github.com/mustache.1.html

注:
在Ubuntu下,如果没有安装ruby,请安装。如:

$ sudo apt-get install ruby rubygems
$ sudo gem install mustache

之后,它是安装到了/var/lib/gems/1.8/bin/。调用/var/lib/gems/1.8/bin/mastache或者加入路径吧。

本文的内容感谢@shellexy的帮助。