基本使用方法:數組
{ "name": "Chris", "company": "<b>GitHub</b>" }
template | output |
一、 {{name}} 二、 {{age}} //數據中沒有age,輸出爲空 三、 {{company}} //會轉義 四、 {{{company}}} //不會轉義 五、 {{&company}}
|
一、 Chris
二、
三、 <b>GitHub</b>
四、 <b>GitHub</b>
五、 <b>GitHub</b>
六、 {{company}}
|
怎麼定義:一個叫作person的Section: {{#person}}這裏的內容都是屬於person這個section的代碼段:block {{/person}}函數
一、若是person變量不存在,或者person的值爲null
, undefined
, false
, 0
, NaN
, empty string or an empty listthis
那麼這個Section之間的全部內容都不會顯示。這個能夠用來控制那些代碼片斷不顯示。spa
二、若是person變量的值不爲null
, undefined
, 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
|
{{^person}}code block {{/person}}io
這個和section相似,區別是:當person的值爲null
, undefined
, false
, falsy or an empty list.的時候纔會渲染。