简体   繁体   English

车把模板无法正常工作

[英]Handlebars template not working properly

I am testing out Handlebars templates in my express/node.js app. 我正在Express / node.js应用程序中测试出Handlebars模板。 I wrote the following template & .js files but they are not working properly. 我编写了以下模板&.js文件,但它们无法正常工作。 The page displays the unordered list's 3 bullets, but does not contain the data. 该页面显示无序列表的3个项目符号,但不包含数据。 Does anyone know what might be going on? 有人知道会发生什么吗? My console & terminal do not return any errors. 我的控制台和终端不返回任何错误。

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="javascript/templating.js"></script> 
</head>

<h1>Hello World</h1>
<script id="handlebars-test" type="text/x-handlebars-template">
    <ul>
    <li>{{firstName}}</li>
    <li>{{twitter}}</li>
    <li>{{jobTitle}}</li>
    </ul>
</script>
<div id="main"></div>



$(document).ready(function() {
console.log('yo!!');
var source = $("#handlebars-test").html();
var template = Handlebars.compile(source);

var data = {
    twitter: 'fasdfasdf',
    jobTitle: 'loser',
    firstName: 'bye'
}

console.log(data);
$("#main").append(template(data));
});

Hi you should call the script at the end. 嗨,您应该在最后调用脚本。 This code works fine 此代码可以正常工作

   <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js" type="text/javascript" charset="utf-8"></script>
    </head>

    <h1>Hello World</h1>
    <script id="handlebars-test" type="text/x-handlebars-template">
        <ul>
        <li>{{firstName}}</li>
        <li>{{twitter}}</li>
        <li>{{jobTitle}}</li>
        </ul>
    </script>
    <div id="main"></div>
    <script type="text/javascript">
      $(document).ready(function() {
            var source = $("#handlebars-test").html();
            var template = Handlebars.compile(source);

            var data = {
                twitter: 'fasdfasdf',
                jobTitle: 'loser',
                firstName: 'bye'
            }
            var theCompiledHtml = template(data);

            // Add the compiled html to the page
            $('#main').html(theCompiledHtml);
        });  
    </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM