基本使用方法:数组
{ "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.的时候才会渲染。