mustache語法整理

基本使用方法:數組

{
  "name": "Chris",
  "company": "<b>GitHub</b>"
}
template output
一、 {{name}} 二、 {{age}} //數據中沒有age,輸出爲空 三、 {{company}} //會轉義 四、 {{{company}}} //不會轉義 五、 {{&company}}
六、輸出{{}} {{
=<% %>=}} {{company}} <%={{ }}=%>

七、註釋:{{! }}

 

一、 Chris
二、
三、 &lt;b&gt;GitHub&lt;/b&gt;
四、 <b>GitHub</b>
五、 <b>GitHub</b>
六、 {{company}}

 

Sections

怎麼定義:一個叫作person的Section: {{#person}}這裏的內容都是屬於person這個section的代碼段:block {{/person}}函數

一、若是person變量不存在,或者person的值爲nullundefinedfalse0, NaN, empty string or an empty listthis

那麼這個Section之間的全部內容都不會顯示。這個能夠用來控制那些代碼片斷不顯示spa

二、若是person變量的值不爲nullundefined, or false, and is not an empty list,那麼這個block會被渲染。若是是數組,會迭代渲染code

 其中,有幾種經典用法:對象

說明 數據 template output
當循環數組時  . 表示current item in the list
{
  "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"]
}

 

{{#musketeers}}
* {{.}}
{{/musketeers}}

 

* Athos
* Aramis
* Porthos
* D'Artagnan

 

輸入的數據能夠爲函數,這時候調用這個數據blog

就能夠達到調用函數的功能。string

this以前遍歷的當前對象it

 
     
{
  "beatles": [ { "firstName": "John", "lastName": "Lennon" }, { "firstName": "Paul", "lastName": "McCartney" }, { "firstName": "George", "lastName": "Harrison" }, { "firstName": "Ringo", "lastName": "Starr" } ], "name": function () { return this.firstName + " " + this.lastName; } }
 
    
 
{{#beatles}}
* {{name}}
{{/beatles}}

 

 
* John Lennon
* Paul McCartney
* George Harrison
* Ringo Starr

 

 

Inverted Sections

{{^person}}code block {{/person}}io

這個和section相似,區別是:當person的值爲nullundefinedfalsefalsy or an empty list.的時候纔會渲染。

相關文章
相關標籤/搜索