![]() |
mustache /m'ʌst,æʃ/ n. 髭,胡子; 圖片來源 SMOSH.COM |
舉例來說:
- "Hello, ${who}" //Groovy
- "Hello, #{who}" #Ruby
- "Hello, %s" % who #Python
- "Hello, $who" //PHP
但是有些語言並沒有類似的簡易方法,例如:
- "Hello, " + who //JavaScript, etc...
這對於經常需要切換語言,或者同時需要使用多種語言的開發者來說,可能會造成一點小困擾。
如果想要追求統一的寫法,那就可以看看「Mustache」這個鬍子牌的解決方案。
Mustache 提供 Ruby, JavaScript, Python, Erlang, PHP, Perl, Objective-C, Java, .NET, Android, C++, Go, Lua, ooc, ActionScript, ColdFusion, Scala, Clojure, Fantom, CoffeeScript, D, 以及 node.js 等不同版本實作。
Mustache 是鬍子的意思,所以在程式字串中要嵌入變數,就是用很像鬍子的符號 "{{" 起始與 "}}" 結尾。
Mustache 是鬍子的意思,所以在程式字串中要嵌入變數,就是用很像鬍子的符號 "{{" 起始與 "}}" 結尾。
Demo
<h1>{{header}}</h1>
{{#bug}}
{{/bug}}
{{#items}}
{{#first}}
<li><strong>{{name}}</strong></li>
{{/first}}
{{#link}}
<li><a href="{{url}}">{{name}}</a></li>
{{/link}}
{{/items}}
{{#empty}}
<p>The list is empty.</p>
{{/empty}}
Value Set in JSON
{
"header": "Colors",
"items": [
{"name": "red", "first": true, "url": "#Red"},
{"name": "green", "link": true, "url": "#Green"},
{"name": "blue", "link": true, "url": "#Blue"}
],
"empty": false
}
沒有留言:
張貼留言