dust学习

来源:互联网 发布:华策数据科技有限公司 编辑:程序博客网 时间:2024/06/06 18:04
学习链接:https://github.com/linkedin/dustjs/wiki/Dust-Tutorial
测试链接:http://www.dustjs.com/test/test.html


1.变量解析

 {name:'zhang'} <div>{name}</div>>>>>> <div></div>

2.数组遍历
 ['jack','bob','tom'] <div>{.} </div>>>>> <div>jack bob tom </div>

3.json对象(数组)
{friends:[    {name:'tom'},    {name:'jack'},    {name:'jone'}  ] }{#friends}    <div>{name} - {$idx}-{$len}</div>{/friends}>>>><div>tom - 0 - 3</div><div>jack - 1 - 3</div><div>jone - 2 - 3</div>

4.获取数组长度

{friends:[    {name:'tom'},    {name:'jack'},    {name:'jone'}  ] }{@size key=friends/}>>>>3

5.父子作用域
{A:{    name:'A',    B:{        name:'B'    }  }}{#A.B} B的scope:{name}----A的scope:{A.name}{/A.B}>>>>B的scope:B ---- A的scope:A

6.兄弟作用域(如果A中有type,会覆盖掉B中type)
{A:{name:'A'},B:{type:'TB'}}{#A:B} A的name:{name},B的type:{type}{/A}>>>>A的name:A,B的type:TB

7. ?(true)和^(false)和:else使用
{?A}  这里A为true  {:else}  这里A为false{/A}>>>>{^A}    这里A为false{:else}    这里A为true{/A}


8.{@select key="xxx"} + @eq, @ne, @lt, @lte, @gt, @gte, @default
{@select key=foo}  {@any}Congratulations! You got: {/any}  //任何一个满足就会展示any中内容  {@eq value="1"}First Place{/eq}  {@eq value="2"}Second Place{/eq}  {@eq value="3"}Third Place{/eq}  {@none}Better luck next time!{/none}   //所有都不满足就会展示none中内容{/select}


9.{@math}

{@math key="16" method="add" operand="4"/}  - Result will be 20{@math key="16.5" method="floor"/} - Result will be 16{@math key="16.5" method="ceil"/} - Result will be 17{@math key="-8" method="abs"/} - Result will be 8{@math key="{$idx}" method="mod" operand="2"/} - Return 0 or 1 according to $idx value


10.{@math}输出的结果最为输入

{@math key="13" method="add" operand="12"} {@gt value=123}  13 + 12 > 123 {/gt} {@none}  Math is fun {/none}{/math}

11.{@if condition}

//测试后,发现@if不起作用。

12.{@sep},忽略数组中中后一项的处理

foo:[{name:'A'},{name:'B'},{name:'C'}]{#foo} {name}{@sep},{/sep}{/foo}>>>>A,B,C

原创粉丝点击