mustache模板引擎,mustache模板
Logic-less templates.
github:http://mustache.github.com/
文檔:http://mustache.github.com/mustache.5.html
http://mustache.github.io
Java 版 Mustache SVN 源碼
http://jmustache.googlecode.com/svn
mustache 類似 freemark和valicity 模板引擎,不過mustache 更輕量級,支援語言: Ruby, JavaScript, Python,Erlang, node.js, PHP, Perl, Perl6, Objective-C, Java, C#/.NET, Android, C++, Go, Lua,ooc, ActionScript, ColdFusion, Scala, Clojure,Fantom, CoffeeScript, D, Haskell, XQuery,ASP, Io, Dart, Haxe, Delphi, Racket, Rust,OCaml, Swift, and for Bash
mustache 支援功能比較弱,不過我們可以建立在mustache 之上進行擴充實現。
mustache的特點就是很文法很簡單,主要文法如下
- {{ name }} 列印變數,預設是escape過的,如果不要escape,用3個分隔字元 {{{ name }}},或者用 {{ &name }},這個和分隔字元無關
- {{#person}}…{{/person}} 區塊,4種方式
- person是真假值,決定是否輸出
- person 是list of array,會迴圈展開 for x in person:section.render('xxx)
- person 是匿名函數/object, 區塊包裹的html 會作為參數傳遞進去
- person 是dict,直接列印 dict[key]
- {{^person}}…{{/person},反向區塊
- {{!name }} 注釋
- {{> box }} 載入子模組
Template:
{{#person?}} Hi {{name}}!{{/person?}}
Hash:
{ "person?": { "name": "Jon" }}
Output:
Hi Jon!
Template:
{{#repo}} <b>{{name}}</b>{{/repo}}{{^repo}} No repos :({{/repo}}
Hash:
{ "repo": []}
Output:
No repos :(