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彩蛋:
- <!--
- _ _
- _____*~~~ ** ~~~*_____
- __* ___ |/__/| ___ *__
- _* / 888~~/__(8OO8)__/~~888 / *_
- _* /88888888888888888888888888/ *_
- * |8888888888888888888888888888| *
- /~* /8888/~/88/~/8888/~/88/~/8888/ *~
- / ~* /88/ // (88) // /88/ *~
- / ~* // // // *~
- / ~~*_ _*~~/
- / ~~~~~*___ ** ___*~~~~~ /
- / ~ ~ /
- / /
- / /
- / /
- / t__n__r__ /
- / | ####### |
- / ___ | ####### | ____i__ /
- / _____p_____l_l____ | ####### | | ooooo | qp
- i__p__ / | ############## | | ####### |__l___xp____| ooooo | |~~~~|
- oooo |_I_| ############## | | ####### |oo%Xoox%ooxo| ooooo |p__h__|##%#|
- oooo |ooo| ############## | | ####### |o%xo%%xoooo%| ooooo | |#xx%|
- oooo |ooo| ############## | | ####### |o%ooxx%ooo%%| ooooo |######|x##%|
- oooo |ooo| ############## | | ####### |oo%%x%oo%xoo| ooooo |######|##%x|
- oooo |ooo| ############## | | ####### |%x%%oo%/oo%o| ooooo |######|/#%x|
- oooo |ooo| ############## | | ####### |%%x/oo/xx%xo| ooooo |######|#%x/|
- oooo |ooo| ############## | | ####### |xxooo%%/xo%o| ooooo |######|#^x#|
- oooo |ooo| ############## | | ####### |oox%%o/x%%ox| ooooo |~~~$~~|x##/|
- oooo |ooo| ############## | | ####### |x%oo%x/o%//x| ooooo |_KKKK_|#x/%|
- ooo~/|ooo|~/############## | ~/####### |oox%xo%%oox%~/ooooo |_|~|~/|xx%/|
- ooo ||oHo| |####AAAA###### |h||##XX### |x%x%WWx%%/ox||ooDoo |_| |Y||xGGx|
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~
- -->
个人觉得比smarty好用多了。不为别的,因为简洁明了。
传闻 豆瓣说(http://shuo.douban.com) 运用了这种模板?关注中
简要介绍下用法:
1A typical Mustache template:
- Hello {{name}}
- You have just won ${{value}}!
- {{#in_ca}}
- Well, ${{taxed_value}}, after taxes.
- {{/in_ca}}
Given the following 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.
Mustache可以用在包括html 配置文件 源代码之类的任何地方。通过提供hash或者对象可以渲染出模板中的变量。模板没有if-else,for-loop标记,只有标记(tag)。
常用标签有类似{{name}},{{#person}}这样语法的标签.如果不提供值,将不会渲染出来。{{{html}}}和{{& html}}将会渲染出没有转义的html内容。
区域渲染通过{{#person}} ... {{/person}}来实现。例如
- Shown.
- {{#nothin}}
- Never shown!
- {{/nothin}}
输出Shown.(如果没有提供nothin)
如果提供了非空列表或者数组,区域渲染将会重复渲染列表或数组每一项。例如
Template:
- {{#repo}}
- <b>{{name}}</b>
- {{/repo}}
Hash:
- {
- "repo": [
- { "name": "resque" },
- { "name": "hub" },
- { "name": "rip" },
- ]
- }
Output:
- <b>resque</b>
- <b>hub</b>
- <b>rip</b>
另外,Mustache支持lambda表达式
Template:
- {{#wrapped}}
- {{name}} is awesome.
- {{/wrapped}}
Hash:
- {
- "name": "Willy",
- "wrapped": function() {
- return function(text) {
- return "<b>" + render(text) + "</b>"
- }
- }
- }
Output:
- <b>Willy is awesome.</b>
打注释也很方便:{{! ignore me }}
导入别的文件只要像这样:
- base.mustache:
- <h2>Names</h2>
- {{#names}}
- {{> user}}
- {{/names}}
- user.mustache:
- <strong>{{name}}</strong>
便能输出
- <h2>Names</h2>
- {{#names}}
- <strong>{{name}}</strong>
- {{/names}}
另外的另外{{}}也是可以自行配置的!