2 u012536005 u012536005 于 2016.01.19 08:15 提问

nodejs express中handlebars 报错

nodejs express中自定义handlebars helps,页面中写有类似{{fuctt num}}时就是打不开网页

 {{#section "head"}}
    <script type="text/javascript" src="js/jquery-1.11.3.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.min.js"></script>
    <script type="text/javascript" src="js/myjs.js"></script>

        <script id="some-template" type="text/x-handlebars-template">
        <table>
            <thead>
            <th>Username</th>
            <th>Real Name</th>
            <th>Email</th>
            </thead>
            <tbody>
            {{#if users}}
            <tr>
                <td>{{username}}</td>
                <td>{{firstName}} {{lastName}}</td>
                <td>{{email}}</td>
            </tr>
            {{else}}
            <tr>
                <td colspan="3">NO users!</td>
            </tr>
            {{/if}}
            </tbody>
        </table>
    </script>
  <script id="helper-template" type="text/x-handlebars-template">
        <div>
##             <h1>By {{fullName author}}</h1>****/*此处报错打不开网页,备注后能打开*/****
            <div>{{body}}</div>

            <h1>Comments</h1>

            {{#each comments}}
            <h2>By {{!-- {{fullName author}} --}}</h2>
            <h2>{{body}}</h2>
                {{/each}}
            </div>
    </script>
{{/section}}
 <h2>Simple handlebars demo</h2>
    <button id="btn_simple">Click me</button>
    <div id="my_div">

    </div>
    <h2>Handlebars Helpers demo</h2>
    <button id="btn_helper">Click me</button>
    <div id="helper_div">

    </div>

## **其余引用文档都能加载

## 下面是js文件**

     $(document).ready(function(){  
        Handlebars.registerHelper('fullName', function(person) {  
          return person.firstName + "这就对了 " + person.lastName;
        });  
      $("#btn_simple").click(function(){  

          alert("first button")
        showTemplate();  
      });  
       $("#btn_helper").click(function(){  

        showHowUseHelper();  
      });  
    });  
    // var context = {title: "My New Post", body: "This is my first post!"};  
    var persion = {title :"My New Post",body:"This is my first post!"}  
    function showTemplate(){  
        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" }  
            ]};  
          $("#my_div").html(template(data));;  
    }  

    function showHowUseHelper(){  
        var context = {  
          author: {firstName: "Alan", lastName: "Johnson"},  
          body: "I Love Handlebars",  
          comments: [{  
            author: {firstName: "Yehuda", lastName: "Katz"},  
            body: "Me too!"  
          }]  
        };  
        var source   = $("#helper-template").html();  
        var template = Handlebars.compile(source);  
        $("#helper_div").html(template(context));;  

    }  

3个回答

zangjiali
zangjiali   2016.01.19 13:06
已采纳
\{{fullName author}} 

我们必须转义至少一个大括号,否则,服务器端视图会尝试对其进行替换。

dcxy0
dcxy0   Ds   Rxr 2016.01.19 09:45

最近也刚好在学nodejs,把你代码传到百度云看下吧,这样看不出来问题,或者贴一下报错的图

zangjiali
zangjiali   2016.01.19 13:05

{{fullName author}} 我们必须转义至少一个大括号,否则,服务器端视图会尝试对其进行替换。

zangjiali
zangjiali \\{{fullName author}}
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!