美文网首页
handlebars

handlebars

作者: osoLife | 来源:发表于2017-06-21 23:09 被阅读0次

快速开始

<script type="text/x-handlebars-template" id="art-tpl">
    <div class="article">
        <h1>{{title}}</h1>
        <div class="content">
            {{content}}
        </div>
    </div>
</script>

var data={
    title:"hello",
    content:"osoLife"
};
var source=$("#art-tpl").html();
var tpl=Handlebars.compile(source);
var html=tpl(data);

each的基本使用

<table border="1">
    <thead>
        <tr>
           <th>姓名</th>
           <th>性别</th>
           <th>年龄</th>
        </tr>
    </thead>
    <tbody>
            
    </tbody>
</table>

<script type="text/x-handlebars-template" id="table-tpl">
    {{#each student}}
        <tr>
          <td>{{name}}</td>
          <td>{{sex}}</td>
          <td>{{age}}</td>
        </tr>
    {{/each}}
</script>

var data={
    "student":[
                {
                    "name":"moli",
                    "sex":"男",
                    "age":"18"
                },              
                {
                    "name":"osoLife",
                    "sex":"男",
                    "age":"28"
                },
                {
                    "name":"michael",
                    "sex":"男",
                    "age":"38"
                }
    ]
};
var tpl=Handlebars.compile($("#table-tpl").html());
$("tbody").html(tpl(data));

相关文章

网友评论

      本文标题:handlebars

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