1、下载jsrender.js
2、简单使用
页面上:
<table>
<thead>
<tr>
<th >序号</th>
<th>评论</th>
</tr>
</thead>
<tbody>
<!--模板渲染的位置-->
</tbody>
</table>
使用模板引擎
<!-- 引入模板引擎 -->
<script src="/static/assets/vendors/jsrender/jsrender.js"></script>
<!-- 使用模板引擎 -->
<script id="comments_tmpl" type="text/x-jsrender">
{{for comments}}
<tr>
<td>{{:#index}}</td>
<td>{{:content}}</td>
</tr>
{{/for}}
</script>
发送AJAX请求得到数据并渲染到列表上
$.getJSON('/admin/api/comments.php',{},function(res){
var html=$('#comments_tmpl').render({
comments:res
})
//将数据渲染到页面上
$('tbody').html(html)
})