程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> handlebars express-nodejs express中handlebars 報錯

handlebars express-nodejs express中handlebars 報錯

編輯:編程解疑
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: "[email protected]" },  
              {username: "allison", firstName: "Allison", lastName: "House", email: "[email protected]" },  
              {username: "ryan", firstName: "Ryan", lastName: "Carson", email: "[email protected]" }  
            ]};  
          $("#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));;  

    }  

最佳回答:


\{{fullName author}} 

我們必須轉義至少一個大括號,否則,服務器端視圖會嘗試對其進行替換。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved