Handlebars的使用

web 开发中,js 解析JSON 是经常的事情。非常繁琐。handlebars 使用了模版,只要你定义一个模版,提供一个json对象,handlebars 就能吧json对象放到你定的模版中,非常方便好用! 下面直接上代码:
 

Html代码  收藏代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Handlebars demo</title>  
  6. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>  
  7. <script type="text/javascript" src="js/handlebars-1.0.0.beta.6.js"></script>  
  8. <script type="text/javascript" src="js/myjs.js"></script>  
  9. <style type="text/css"></style>  
  10. </head>  
  11. <body>  
  12.     <h2>Simple handlebars demo</h2>  
  13.     <button id="btn_simple">Click me</button>  
  14.     <div id="my_div">  
  15.   
  16.     </div>  
  17.     <h2>Handlebars Helpers demo</h2>  
  18.     <button id="btn_helper">Click me</button>  
  19.     <div id="helper_div">  
  20.   
  21.     </div>  
  22.     <script id="some-template" type="text/x-handlebars-template">  
  23.           <table>  
  24.             <thead>  
  25.               <th>Username</th>  
  26.               <th>Real Name</th>  
  27.               <th>Email</th>  
  28.             </thead>  
  29.             <tbody>  
  30.               {{#if users}}  
  31.                 <tr>  
  32.                   <td>{{username}}</td>  
  33.                   <td>{{firstName}} {{lastName}}</td>  
  34.                   <td>{{email}}</td>  
  35.                 </tr>  
  36.               {{else}}  
  37.                 <tr>  
  38.                   <td colspan="3">NO users!</td>  
  39.                 </tr>  
  40.               {{/if}}  
  41.             </tbody>  
  42.           </table>  
  43.     </script>  
  44.     <script id="helper-template" type="text/x-handlebars-template">  
  45.         <div>  
  46.           <h1>By {{fullName author}}</h1>  
  47.           <div>{{body}}</div>  
  48.   
  49.           <h1>Comments</h1>  
  50.   
  51.           {{#each comments}}  
  52.           <h2>By {{fullName author}}</h2>  
  53.           <div>{{body}}</h2>  
  54.           {{/each}}  
  55.         </div>  
  56.     </script>  
  57. </body>  
  58. </html>  

 

Js代码  收藏代码

  1. $(document).ready(function(){  
  2.     Handlebars.registerHelper('fullName', function(person) {  
  3.       return person.firstName + " " + person.lastName;  
  4.     });  
  5.   $("#btn_simple").click(function(){  
  6.     // $(this).hide();  
  7.     showTemplate();  
  8.   });  
  9.    $("#btn_helper").click(function(){  
  10.   
  11.     showHowUseHelper();  
  12.   });  
  13. });  
  14. // var context = {title: "My New Post", body: "This is my first post!"};  
  15. var persion = {title :"My New Post",body:"This is my first post!"}  
  16. function showTemplate(){  
  17.     var source   = $("#some-template").html();  
  18.     var template = Handlebars.compile(source);  
  19.       var data = { users: [  
  20.           {username: "alan", firstName: "Alan", lastName: "Johnson", email: "alan@test.com" },  
  21.           {username: "allison", firstName: "Allison", lastName: "House", email: "allison@test.com" },  
  22.           {username: "ryan", firstName: "Ryan", lastName: "Carson", email: "ryan@test.com" }  
  23.         ]};  
  24.       $("#my_div").html(template(data));;  
  25. }  
  26.   
  27. function showHowUseHelper(){  
  28.     var context = {  
  29.       author: {firstName: "Alan", lastName: "Johnson"},  
  30.       body: "I Love Handlebars",  
  31.       comments: [{  
  32.         author: {firstName: "Yehuda", lastName: "Katz"},  
  33.         body: "Me too!"  
  34.       }]  
  35.     };  
  36.     var source   = $("#helper-template").html();  
  37.     var template = Handlebars.compile(source);  
  38.     $("#helper_div").html(template(context));;  
  39.       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值