Javascript 語言的模板引擎:Mustache

Web 模板引擎是爲了使用戶界面與業務數據(內容)分離而產生的,它可以生成特定格式的文檔,通常是標準的 HTML 文檔。當然不同的開發語言有不同模板引擎,如 Javascript 下的 Hogan 、ASP 下的 aspTemplate、以及 PHP 下的 Smarty,這裏主要介紹基於 Javascript 語言的模板引擎,目前流行有 Mustache、Hogan、doT.js、JsRender、Kendo UI Templates等,jsperf.com 上可以看到它們的性能對比,首先先介紹下 Mustache

一、Mustache 簡介:

Mustache 是一個 logic-less (輕邏輯)模板解析引擎,它的優勢在於可以應用在 Javascript、PHP、Python、Perl 等多種編程語言中。

二、Mustache 語法:

Mustache 的模板語法很簡單,就那麼幾個:

  • {{keyName}}
  • {{#keyName}} {{/keyName}}
  • {{^keyName}} {{/keyName}}
  • {{.}}
  • {{<partials}}
  • {{{keyName}}}
  • {{!comments}}

這裏將以 javascript 應用爲例進行介紹,先來看個 Demo:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<code>...
<script type="text/javascript" src="mustache.js"></script>
<script type="text/javascript">
var data = {
    "company""Apple",
    "address": {
        "street""1 Infinite Loop Cupertino</br>",
        "city""California ",
        "state""CA ",
        "zip""95014 "
    },
    "product": ["Macbook ","iPhone ","iPod ","iPad "]
}
 
var tpl = '<h1>Hello {{company}}</h1>';
var html = Mustache.render(tpl, data);
 
console.log( html )
</script>
...
 
//運行後 Console 輸出:
 
<h1>Hello Apple</h1></code>

在 Demo 中可以看到 data 是數據,tpl 是定義的模板,Mustache.render(tpl, data)方法是用於渲染輸出最終的 HTML 代碼。

藉助 Demo 來對語法做簡單的介紹:

{{keyName}}

{{}}就是 Mustache 的標示符,花括號裏的 keyName 表示鍵名,這句的作用是直接輸出與鍵名匹配的鍵值,例如:

?
1
2
3
4
var tpl = '{{company}}';
var html = Mustache.render(tpl, data);
//輸出:
Apple

{{#keyName}} {{/keyName}}

#開始、以/結束表示區塊,它會根據當前上下文中的鍵值來對區塊進行一次或多次渲染,例如改寫下 Demo 中的 tpl:

?
1
2
3
4
5
var tpl = '{{#address}} <p>{{street}},{{city}},{{state}}</p> {{/address}}';
var html = Mustache.render(tpl, data);
 
//輸出:
<p>1 Infinite Loop Cupertino&lt;/br&gt;,California,CA</p>

注意:如果{{#keyName}} {{/keyName}}中的 keyName 值爲 null, undefined, false;則不渲染輸出任何內容。

{{^keyName}} {{/keyName}}

該語法與{{#keyName}} {{/keyName}}類似,不同在於它是當 keyName 值爲 null, undefined, false 時才渲染輸出該區塊內容。

?
1
2
3
4
var tpl = {{^nothing}}沒找到 nothing 鍵名就會渲染這段{{/nothing}};
var html = Mustache.render(tpl, data);
//輸出:
沒找到 nothing 鍵名就會渲染這段

{{.}}

{{.}}表示枚舉,可以循環輸出整個數組,例如:

?
1
2
3
4
var tpl = '{{#product}} <p>{{.}}</p> {{/product}}';
var html = Mustache.render(tpl, data);
//輸出:
<p>Macbook </p> <p>iPhone </p> <p>iPod </p> <p>iPad </p>

{{>partials}}

>開始表示子模塊,如{{> address}};當結構比較複雜時,我們可以使用該語法將複雜的結構拆分成幾個小的子模塊,例如:

?
1
2
3
4
5
6
var tpl = "<h1>{{company}}</h1> <ul>{{>address}}</ul>"
var partials = {address: "{{#address}}<li>{{street}}</li><li>{{city}}</li><li>{{state}}</li><li>{{zip}}</li>{{/address}}"}
var html = Mustache.render(tpl, data, partials);
//輸出:
<h1>Apple</h1>
<ul><li>1 Infinite Loop Cupertino&lt;/br&gt;</li><li>California</li><li>CA</li><li>95014</li></ul>

{{{keyName}}}

{{keyName}}輸出會將等特殊字符轉譯,如果想保持內容原樣輸出可以使用{{{}}},例如:

?
1
2
3
var tpl = '{{#address}} <p>{{{street}}}</p> {{/address}}'
//輸出:
<p>1 Infinite Loop Cupertino</br></p>

{{!comments}}

!表示註釋,註釋後不會渲染輸出任何內容。

?
1
2
{{!這裏是註釋}}
//輸出:

參考文章:

http://coenraets.org/blog/2011/12/tutorial-html-templates-with-mustache-js/
http://mustache.github.com/mustache.5.html
http://ued.xinyou.com/2012/07/mustache_5_document.html


來自:http://www.iinterest.net/2012/09/12/web-template-engine-mustache/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章