handlebars.js不会呈现文档

时间:2013-05-29 16:17:58

标签: javascript templates handlebars.js

我遇到了handlebars.js的问题。也许这完全是微不足道的,但我也是全新的车把。所以...这里是我的index.html中的示例代码,如下所示:

    <!DOCTYPE html>

    <!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
    <!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
    <!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
    <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
        <head>

            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
            <title></title>

            <meta name="description" content="">
            <meta name="viewport" content="width=device-width">
            <script data-main="scripts/main" src="scripts/require.js"></script>
            <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->

            <link rel="stylesheet" href="css/normalize.css">
            <link rel="stylesheet" href="css/main.css">
            <script src="scripts/modernizr-2.6.2.min.js"></script>


            <script src='scripts/content.js'></script>
            <script src='scripts/handlebars-1.0.0-rc.4.js'></script>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
            <script>window.jQuery || document.write('<script src="scripts/jquery-1.9.1.min.js"><\/script>')</script>
            <script src="scripts/plugins.js"></script>
            <script src="scripts/main.js"></script>


        </head>

        <body>

            <!--[if lt IE 7]>
                <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
            <![endif]-->

            <!-- Add your site or application content here -->

<script id="some-template" type="text/x-handlebars-template">
      <table>
        <thead>
          <th>Username</th>
          <th>Real Name</th>
          <th>Email</th>
        </thead>
        <tbody>
          {{#users}}
            <tr>
              <td>{{username}}</td>
              <td>{{firstName}} {{lastName}}</td>
              <td>{{email}}</td>
            </tr>
          {{/users}}
        </tbody>
      </table>
    </script>


        </body>
    </html>

还有这个content.js文件包含简单的javascript代码,但它仍然无法正常工作。如果你们中的任何人可以提出建议或给予解决方案我会非常感激。提前谢谢

 var source   = $("#some-template").html();
  var template = Handlebars.compile(source);
  var data = { users: [
      {username: "alan", firstName: "Alan", lastName: "Johnson", email: "alan@test.com" },
      {username: "allison", firstName: "Allison", lastName: "House", email: "allison@test.com" },
      {username: "ryan", firstName: "Ryan", lastName: "Carson", email: "ryan@test.com" }
    ]};
  $("#content-placeholder").html(template(data));

祝你好运

2 个答案:

答案 0 :(得分:1)

除非你创建了一个名为“users”的自定义块助手迭代器,否则下面的块将不会执行,这就是为什么当你尝试渲染模板时看不到任何东西。

块助手迭代器不正确:

{{#users}}
   ...
{{/users}}

根据您在示例中提供的数据,您应该执行以下操作:

正确的块助手迭代器:

{{#each user}}
   My name is {{this.firstname}} {{this.lastname}} and my email is {{this.email}}.
{{/each}}

您可以在Handlebarsjs文档站点上引用块帮助程序。一开始消化可能有点挑战,但是玩一下你会开始学习一些Handlebarsjs功能。

答案 1 :(得分:0)

  1. 您在导入jQuery之前导入了“content.js”脚本

  2. 该脚本中的代码应该包装在jQuery“ready”处理程序中:

     $(function() {
        var source   = $("#some-template").html();
        var template = Handlebars.compile(source);
        var data = { users: [
          {username: "alan", firstName: "Alan", lastName: "Johnson", email: "alan@test.com" },
          {username: "allison", firstName: "Allison", lastName: "House", email: "allison@test.com" },
          {username: "ryan", firstName: "Ryan", lastName: "Carson", email: "ryan@test.com" }
        ]};
        $("#content-placeholder").html(template(data));
     });
    
  3. 第一个问题会导致在浏览器的控制台中记录错误。这是你应该总是先检查的东西。