关于模板语言 Mustache

来源:互联网 发布:战争之王 知乎 编辑:程序博客网 时间:2024/05/16 14:56

@Fenng A client-rendering framework for Facebook by Changhao Jianghttp://t.co/NIo6vCd

 

Fenng推荐了一款模板语言:mustache(意思是胡须)。

 

mustache官网是这么介绍的:

 

Logic-less templates.

Available in Ruby, JavaScript, Python,Erlang, PHP, Perl, Objective-C, Java, .NET,Android, C++, Go, Lua, ooc, ActionScript,ColdFusion, Scala, Clojure, Fantom,CoffeeScript, D, and for node.js.

Works great with TextMate, Vim, Emacs, andCoda.

The Manual: mustache(5) and mustache(1)

 

总而言之,支持很多语言,作者是facebook的蒋博士。

 

全部用法详见http://mustache.github.com/mustache.5.html

github的html彩蛋:

 

个人觉得比smarty好用多了。不为别的,因为简洁明了。

传闻 豆瓣说(http://shuo.douban.com) 运用了这种模板?关注中

简要介绍下用法:

 

1A typical Mustache template:

 

 

Given the following hash:

 

Will produce the following:

 
Mustache可以用在包括html 配置文件 源代码之类的任何地方。通过提供hash或者对象可以渲染出模板中的变量。模板没有if-else,for-loop标记,只有标记(tag)。
常用标签有类似{{name}},{{#person}}这样语法的标签.如果不提供值,将不会渲染出来。{{{html}}}和{{& html}}将会渲染出没有转义的html内容。
区域渲染通过{{#person}} ... {{/person}}来实现。例如
 
输出Shown.(如果没有提供nothin)
如果提供了非空列表或者数组,区域渲染将会重复渲染列表或数组每一项。例如

Template:

 

Hash:

 

Output:

 
另外,Mustache支持lambda表达式

Template:

 

Hash:

 

Output:

 
打注释也很方便:{{! ignore me }}
导入别的文件只要像这样:
 
便能输出
 
另外的另外{{}}也是可以自行配置的!
好像没有更多要介绍了,就这么多了。和python一样,简洁明了。

 

原创粉丝点击