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

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

打赏

发表评论

电子邮件地址不会被公开。 必填项已用*标注