美文网首页
模板引擎dot.js

模板引擎dot.js

作者: 黑夜_Eric | 来源:发表于2018-12-13 11:18 被阅读0次

    一、基本符号

    {{= }} 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 赋值

    格式:
    {{= }}
    
    1. 数据源:
    {"name":"Jake","age":31}
    
    1. 区域:
    <div id="interpolation"></div>
    
    1. 模板:
    <script id="interpolationtmpl" type="text/x-dot-template">
        <div>Hi {{=it.name}}!</div>
        <div>{{=it.age || ''}}</div>
    </script>
    
    1. 调用方式:
    var dataInter = {"name":"Jake","age":31};
    var interText = doT.template($("#interpolationtmpl").text());
    $("#interpolation").html(interText(dataInter));
    
    • 实例2

    for evaluation for in 循环

    1. 格式:
    {{ for var key in data { }} 
    {{= key }} 
    {{ } }}
    
    1. 数据源:
    {"name":"Jake","age":31,"interests":["basketball","hockey","photography"],"
    contact":{"email":"jake@xyz.com","phone":"999999999"}}
    

    c

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

    5.调用方式:

    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 数组

    1. 格式:
    {{~data.array :value:index }}
        ...
    {{~}}
    
    1. 数据源:
    {"array":["banana","apple","orange"]}
    
    1. 区域:
    <div id="arrays"></div>
    
    1. 模板:
    <script id="arraystmpl" type="text/x-dot-template">
        {{~it.array:value:index}}
            <div>{{= index+1 }}{{= value }}!</div>
        {{~}}
    </script>
    

    5.调用方式:

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

    {{? }} for conditionals 条件

    1. 格式:
    {{? }} if
    {{?? }} else if
    {{??}} else
    
    1. 数据源:
    {"name":"Jake","age":31}
    
    1. 区域:
    <div id="condition"></div>
    
    1. 模板:
    <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>
    

    5.调用方式:

    var dataEncode = {"uri":"http://jq22.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

    1. 格式:
    {"uri":"[http://jq22.com/?keywords=Yoga](http://jq22.com/?keywords=Yoga)"}
    
    1. 数据源:
    {{!it.uri}}
    
    1. 区域:
    <div id="encode"></div>
    
    1. 模板:
    <script id="encodetmpl" type="text/x-dot-template">
        Visit {{!it.uri}} {{!it.html}}
    </script>
    

    5.调用方式:

    var dataEncode = {"uri":"http://jq22.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));
    

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

    {{## #}} for compile-time defines

    1. 数据源:
    {"name":"Jake","age":31}
    
    1. 区域:
    <div id="part"></div>
    
    1. 模板:
    <script id="parttmpl" type="text/x-dot-template">
        {{##def.snippet:
        <div>{{=it.name}}</div>{{#def.joke}}
        #}}
        {{#def.snippet}}
        {{=it.html}}
    </script>
    

    4.调用方式:

    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));
    

    相关文章

      网友评论

          本文标题:模板引擎dot.js

          本文链接:https://www.haomeiwen.com/subject/kgxvhqtx.html