1. 程式人生 > >關於模板語言 Mustache

關於模板語言 Mustache

@Fenng A client-rendering framework for Facebook by Changhao Jiang

Fenng推薦了一款模板語言:mustache(意思是鬍鬚)。

mustache官網是這麼介紹的:

總而言之,支援很多語言,作者是facebook的蔣博士。

 

個人覺得比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一樣,簡潔明瞭。