mustache.js使用基本(三)

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

zccst

本節要點是子模組(partials)和分隔字元(delimiter)等

1,子模組(partials)

/*{{>partials}}以>開始表示子模組,如{{> address}};當結構比較複雜時,我們可以使用該文法將複雜的結構拆分成幾個小的子模組,例如:*/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>{{company}}</h1> <ul>{{>address}}</ul>";//子模板寫法1:定義一個子模組對象。裡面放子模板。var partials = {    address: "{{#address}}<li>{{street}}</li><li>{{city}}</li><li>{{state}}</li><li>{{zip}}</li>{{/address}}"}var html = Mustache.render(tpl, data, partials);//子模板寫法2:直接寫在第三個參數裡var html = Mustache.render(tpl, data, {address: "{{#address}}<li>{{street}}</li><li>{{city}}</li><li>{{state}}</li><li>{{zip}}</li>{{/address}}"});document.getElementById("target").innerHTML = html;

再舉一例:

var view = {    name: "Joe",    winnings: {        value: 1000    }};var template = "Welcome, {{name}}! {{#winnings}} {{>winnings}}{{/winnings}} "var partials = {    winnings: "You just won ${{value}}"};var output = Mustache.to_html(template, view, partials);document.getElementById("target").innerHTML = output;

輸出結果是:

Welcome, Joe! You just won $1000

 

 

2,分隔字元(delimiter)

(1)寫法1:預設的標籤風格

* {{ default_tags }}
{{=<% %>=}}

(2)寫法2:使用ERB風格
* <% erb_style_tags %>
<%={{ }}=%>

(3)寫法3:
* {{ default_tags_again }}

 

3,預解析或緩衝parse

mustache.js會在第一次讀模板時緩衝,第二次可加快速度,如果需要

Mustache.pars(template);

Mustache.render(template, view);

 

 

4,對應的外掛程式

有jQuery,MooTools,Dojo,YUI,qooxdoo。可以使用Rake來built。

 

5,測試(單元測試和整體測試) 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.