關於範本語言 Mustache

來源:互聯網
上載者:User

@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彩蛋:

<!--<br /> _ _<br /> _____*~~~ ** ~~~*_____<br /> __* ___ |/__/| ___ *__<br /> _* / 888~~/__(8OO8)__/~~888 / *_<br /> _* /88888888888888888888888888/ *_<br /> * |8888888888888888888888888888| *<br /> /~* /8888/~/88/~/8888/~/88/~/8888/ *~<br /> / ~* /88/ // (88) // /88/ *~<br /> / ~* // // // *~<br /> / ~~*_ _*~~/<br /> / ~~~~~*___ ** ___*~~~~~ /<br /> / ~ ~ /<br /> / /<br /> / /<br /> / /<br /> / t__n__r__ /<br /> / | ####### |<br /> / ___ | ####### | ____i__ /<br /> / _____p_____l_l____ | ####### | | ooooo | qp<br />i__p__ / | ############## | | ####### |__l___xp____| ooooo | |~~~~|<br /> oooo |_I_| ############## | | ####### |oo%Xoox%ooxo| ooooo |p__h__|##%#|<br /> oooo |ooo| ############## | | ####### |o%xo%%xoooo%| ooooo | |#xx%|<br /> oooo |ooo| ############## | | ####### |o%ooxx%ooo%%| ooooo |######|x##%|<br /> oooo |ooo| ############## | | ####### |oo%%x%oo%xoo| ooooo |######|##%x|<br /> oooo |ooo| ############## | | ####### |%x%%oo%/oo%o| ooooo |######|/#%x|<br /> oooo |ooo| ############## | | ####### |%%x/oo/xx%xo| ooooo |######|#%x/|<br /> oooo |ooo| ############## | | ####### |xxooo%%/xo%o| ooooo |######|#^x#|<br /> oooo |ooo| ############## | | ####### |oox%%o/x%%ox| ooooo |~~~$~~|x##/|<br /> oooo |ooo| ############## | | ####### |x%oo%x/o%//x| ooooo |_KKKK_|#x/%|<br /> ooo~/|ooo|~/############## | ~/####### |oox%xo%%oox%~/ooooo |_|~|~/|xx%/|<br /> ooo ||oHo| |####AAAA###### |h||##XX### |x%x%WWx%%/ox||ooDoo |_| |Y||xGGx|<br /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~<br />--> 

個人覺得比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 ChrisYou 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}}
另外的另外{{}}也是可以自行配置的!
好像沒有更多要介紹了,就這麼多了。和python一樣,簡潔明了。


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.