Simple to use template engine jsrender

1, download jsrender.js
Here Insert Picture Description
2, simple to use

Page on:

 <table>
  <thead>
     <tr>
       <th >序号</th>
       <th>评论</th>
     </tr>
   </thead>
   <tbody>
   <!--模板渲染的位置-->
   </tbody>
 </table>

Use template engine

<!-- 引入模板引擎 -->
  <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 request to get the data and send to the rendered list

    $.getJSON('/admin/api/comments.php',{},function(res){
      var html=$('#comments_tmpl').render({
        comments:res
      })
      //将数据渲染到页面上
     $('tbody').html(html)
    })
Published 43 original articles · won praise 1 · views 3127

Guess you like

Origin blog.csdn.net/u011523953/article/details/104551060