• doT模板


    框架源码地址 https://github.com/olado/doT

    <div id="main">   
    <script id="banner-template" type="text/x-dot-template">
    {{ for(var i=0; i< it.length; i++) { }}
    <div>
            <img src="{{=it.img.url}}" />
            <label><div class="blur"></div>{{=it.name}}</label>
    </div>
    {{ } }}
    </script>
    </div>
    

    {{}} {{=it.}}都是doT.js的一些新写法,简介也从网上搞过来!供大家学习!

    doT.js详细使用介绍

    使用方法:
    {{= }} for interpolation
    {{ }} for evaluation
    {{~ }} for array iteration
    {{? }} for conditionals
    {{! }} for interpolation with encoding
    {{# }} for compile-time evaluation/includes and partials
    {{## #}} for compile-time defines

    调用方式:

    var tmpText = doT.template(模板);  
    tmpText(数据源);
    

    例子一:

    1、for interpolation 赋值
    格式:
    {{= }}

    数据源:{"name":"Jake","age":31}

    区域:

    <div id="interpolation"></div>
    

    模板:

    <script id="interpolationtmpl" type="text/x-dot-template">
    <div>Hi {{=it.name}}!</div>
    <div>{{=it.age || ''}}</div>
    </script>
    

    调用方式:

    var dataInter = {"name":"Jake","age":31};
    var interText = doT.template($("#interpolationtmpl").text());
    $("#interpolation").html(interText(dataInter));
    

    例子二:

    2、for evaluation for in 循环格式:

    {{ for var key in data { }} 
    {{= key }} 
    {{ } }}
    

    数据源:

    {"name":"Jake","age":31,"interests":["basketball","hockey","photography"],"contact":{"email":"jake@xyz.com","phone":"999999999"}}
    

    区域:<div id="evaluation"></div>

    模板:

    <script id="evaluationtmpl" type="text/x-dot-template">
    {{ for(var prop in it) { }}
    <div>KEY:{{= prop }}---VALUE:{{= it[prop] }}</div>
    {{ } }}
    </script>
    

    调用方式:

    var dataEval = {"name":"Jake","age":31,"interests":["basketball","hockey","photography"],"contact":{"email":"jake@xyz.com","phone":"999999999"}};
    var evalText = doT.template($("#evaluationtmpl").text());
    $("#evaluation").html(evalText(dataEval));
    

    例子三:

    3、for array iteration 数组
    格式:

    {{~data.array :value:index }}
    ...
    {{~}}
    

    数据源:{"array":["banana","apple","orange"]}

    区域:<div id="arrays"></div>

    模板:

    <script id="arraystmpl" type="text/x-dot-template">
    {{~it.array:value:index}}
    <div>{{= index+1 }}{{= value }}!</div>
    {{~}}
    </script>
    

    调用方式:

    var dataArr = {"array":["banana","apple","orange"]};
    var arrText = doT.template($("#arraystmpl").text());
    $("#arrays").html(arrText(dataArr));
    

    例子四:

    4、{{? }} for conditionals 条件
    格式:

    {{? }} if
    {{?? }} else if
    {{??}} else
    

    数据源:{"name":"Jake","age":31}

    区域:<div id="condition"></div>

    模板:

    <script id="conditionstmpl" type="text/x-dot-template">
    {{? !it.name }}
    <div>Oh, I love your name, {{=it.name}}!</div>
    {{?? !it.age === 0}}
    <div>Guess nobody named you yet!</div>
    {{??}}
    You are {{=it.age}} and still dont have a name?
    {{?}}
    </script>
    

    调用方式:

    var dataEncode = {"uri":"http://bebedo.com/?keywords=Yoga","html":"<div style='background: #f00; height: 30px; line-height: 30px;'>html元素</div>"};
    var EncodeText = doT.template($("#encodetmpl").text());
    $("#encode").html(EncodeText(dataEncode));
    

    例子五:

    5、for interpolation with encoding

    数据源:{"uri":"http://bebedo.com/?keywords=Yoga"}

    格式:{{!it.uri}}

    区域:<div id="encode"></div>

    模板:

    <script id="encodetmpl" type="text/x-dot-template">
    Visit {{!it.uri}} {{!it.html}}
    </script>
    

    调用方式:

    var dataEncode = {"uri":"http://bebedo.com/?keywords=Yoga","html":"<div style='background: #f00; height: 30px; line-height: 30px;'>html元素</div>"};
    var EncodeText = doT.template($("#encodetmpl").text());
    $("#encode").html(EncodeText(dataEncode));
    

    例子六:

    6、{{# }} for compile-time evaluation/includes and partials
    {{## #}} for compile-time defines

    数据源:{"name":"Jake","age":31}
    区域:<div id="part"></div>
    模板:

    <script id="parttmpl" type="text/x-dot-template">
    {{##def.snippet:
    <div>{{=it.name}}</div>{{#def.joke}}
    #}}
    {{#def.snippet}}
    {{=it.html}}
    </script>
    

    调用方式:

    var dataPart = {"name":"Jake","age":31,"html":"<div style='background: #f00; height: 30px; line-height: 30px;'>html元素</div>"};
    var defPart = {"joke":"<div>{{=it.name}} who?</div>"};
    var partText = doT.template($("#parttmpl").text(), undefined, defPart);
    $("#part").html(partText(dataPart));
    
     
  • 相关阅读:
    【矩阵乘】【DP】【codevs 1305】Freda的道路
    Giraph源代码分析(九)—— Aggregators 原理解析
    Dubbo框架应用之(三)--Zookeeper注冊中心、管理控制台的安装及解说
    Oracle中对数字加汉字的排序(完好)
    PKU-2104-K-th Number
    IE下推断IE版本号的语句
    自己定义View时,用到Paint Canvas的一些温故,简单的帧动画(动画一 ,&quot;掏粪男孩Gif&quot;顺便再提提onWindowFocusChanged)
    <html>
    测试一个网段主机在线情况,禁ping的情况除外
    glance image-create --name "wj_js_company_img" --file a0e1c7fa-d6d3-410f-9bb5-e699e342db91 --disk-format qcow2 --container-format bare --progress --visibility public
  • 原文地址:https://www.cnblogs.com/liuting-1204/p/6000530.html
Copyright © 2020-2023  润新知