翻译API文档,若有疏漏,还望读者不吝赐教。html
英文原文地址git
##名称github
mustache - Logic-less模板.缓存
##概要app
典型Mustache模板示例:less
Hello {{name}} You have just won {{value}} dollars! {{#in_ca}} Well, {{taxed_value}} dollars, after taxes. {{/in_ca}}
hash:oop
{ "name": "Chris", "value": 10000, "taxed_value": 10000 - (10000 * 0.4), "in_ca": true }
输出:this
Hello Chris You have just won 10000 dollars! Well, 6000.0 dollars, after taxes.
##引言翻译
Mustache
可用于HTML,配置文件,源代码等,经过扩展标签(Tags)来渲染模板,标签的值通常是hash或者js对象。code
之因此称为"logic-less",是由于它既没有if-else,也没有for循环,Mustache只有标签。一些标签会被值来替换,有些不会,还有些会被系列的值替换。本文主要介绍不一样类型的Mustache标签。
##TAG类型
标签(Tags) 使用{{ }}
来描述. {{person}}
是一个标签, {{#person}}
也是标签. 在上面两个示例中,咱们将person
做为key值或者标签key,下面咱们来看看不一样类型的tag。
##变量 变量是最基本的Tag类型,模板中的{{name}}
标签会尝试在上下文中寻找名称键值,若是没有找到,则不进行渲染。
全部的变量默认都进行了HTML转义,若是你想返回未转义的HTML,请使用三个花括号{{{name}}}
。
另外你也可使用&
来跳过转义:{{& name}}
,当改变分隔符的时候,这种方式比较有用。(见设置分隔符部分)。
默认状况下,变量缺失会返回一个空字符串。这一般由你所使用的Mustache库设置决定,可是Ruby版本的Mustache则会引起一个异常。例如:
模板:
* {{name}} * {{age}} * {{company}} * {{{company}}}
hash:
{ "name": "Chris", "company": "<b>GitHub</b>" }
输出:
* Chris * * <b>GitHub</b> * <b>GitHub</b>
##块「Sections」 一次或者屡次渲染大段的文本使用块
,这依赖于上下文的键值。
块
使用#
起始,以/
结束。即{{#person}}
表示person
块起始,而{{/person}}
表示结束。块
的表现由键值来决定。
###假值以及空列表 若是person
键存在假值或者空列表,在#
,/
之间的HTML则不会显示。
模板:
Shown. {{#nothin}} Never shown! {{/nothin}}
Hash:
{ "person": true }
输出:
Shown.
###非空列表 若是person
键存在,而且值为真,在#
,/
之间的HTML则会被渲染,而且进行一次或者屡次渲染。
当值为非空列表时,列表中的文本则会被遍历展现。段落的内容则会被赋予到当前的列表项的for each迭代器。经过这种方式,咱们能够遍历集合。
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.
模板:
{{#repo}} <b>{{name}}</b> {{/repo}}
Hash:
{ "repo": [ { "name": "resque" }, { "name": "hub" }, { "name": "rip" } ] }
输出:
<b>resque</b> <b>hub</b> <b>rip</b>
###Lambdas表达式 当值是可调用的对象,例如function
或者lambda
,那么对象会被调用而且跳过文本。 被跳过的文字不进行渲染。{{tags}}
讲不会被展开——由lambda
自身来决定。经过这种方式,能够实现过滤器以及缓存。
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.
模板:
{{#wrapped}} {{name}} is awesome. {{/wrapped}}
Hash:
{ "name": "Willy", "wrapped": function() { return function(text, render) { return "<b>" + render(text) + "</b>" } } }
输出:
<b>Willy is awesome.</b>
###真值
当值为真但不是一个列表的时候,渲染一次。
模板:
{{#person?}} Hi {{name}}! {{/person?}}
Hash:
{ "person?": { "name": "Jon" } }
输出:
Hi Jon!
##反向块「Inverted Sections」 反向块由^
起始,/
结束。即{{^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}} <b>{{name}}</b> {{/repo}} {{^repo}} No repos :( {{/repo}}
Hash:
{ "repo": [] }
Output:
No repos :(
##注释 注释由一个!
开始,以下面的模板所示:
<h1>Today{{! ignore me }}.</h1>
将会被渲染成下面的HTML:
<h1>Today.</h1>
注释支持换行.
##Partials
Partials由一个>
开始,如{{> box}}
.
Partials 仅在运行时(同编译时相反)被渲染,因此(模板)支持循环的Partials,但要避免支持死循环。
Partials 能够继承调用上下文calling context
,在 ERB 中,你可能有以下代码:
<%= partial :next_more, :start => start, :size => size %>
Mustache 仅须要以下:
{{> next_more}}
为何?由于next_more.mustache
文件将会从调用上下文中继承size 和 start 方法
经过这种方式,你能够将partials看做includes,或者模板的扩展,尽管这可能不必定是正确。
例如, 以下的模板和partial:
base.mustache:
<h2>Names</h2> {{#names}} {{> user}} {{/names}}
user.mustache:
<strong>{{name}}</strong>
能够被看出一个文件,扩展的模板:
<h2>Names</h2> {{#names}} <strong>{{name}}</strong> {{/names}}
设置分隔符采用 =
开始,能够将原始的{{
和 }}
修改成自定义的字符
考虑以下的人造示例:
* {{default_tags}} {{=<% %>=}} * <% erb_style_tags %> <%={{ }}=%> * {{ default_tags_again }}
Here we have a list with three items. 下面咱们分别解释三个例子: 第一个使用了默认的分隔标记,第二个经过设置分隔符使用erb style
,第三个经过设置分隔符声明又继续使用默认的分隔符标记
According to ctemplates, this "is useful for languages like TeX, where {{ may occur in the text and are awkward to use for markup."
自定义分隔符不能包含空格或者等号。
版权 Mustache is Copyright (C) 2009 Chris Wanstrath
Original CTemplate by Google