我在项目中使用t.js作为模板引擎。如何为这个json制作模板:
{
data: [
{
FirstName: "Test1",
LastName: "Test11"
},
{
FirstName: "Test2",
LastName: "Test22"
},
{
FirstName: "Test3",
LastName: "Test33"
}
]
}
我想将其渲染为
<ul>
<li>Test1,Test11</li>
<li>Test2,Test22</li>
<li>Test3,Test33</li>
</ul>
你可以为我的上述情况建议任何其他模板引擎以及模板html哪个更好?我不想使用eval or new Func
答案 0 :(得分:1)
标记
<ul id="myList">
{{@mydata}} <li>{{=_val.FirstName}},{{=_val.LastName}}</li> {{/@mydata}}
</ul>
的Javascript
var source = $("#myList").html();
var template = new t(source);
$("#myList").html(template.render({
mydata: [
{
FirstName: "Test1",
LastName: "Test11"
},
{
FirstName: "Test2",
LastName: "Test22"},
{
FirstName: "Test3",
LastName: "Test33"
}
]
}));