Handlebars.js not replacing tags

Go To StackoverFlow.com

2

I'm using Handlebars.js (or trying to) and even with a simple example, it's not replacing the actual tags with the data.

Here's what I have:

    <script id="template" type="text/x-handlebars-template">
     <div>Test Template</div>
     <p>Name: {{name}}</p>
    </script>

    <div id="template-preview"></div>

 <script>
 var source   = $("#template").html();
 var template = Handlebars.compile(source);
 var data = { name : 'first name'};
 $("#template-preview").html(template(data));
</script>

I don't get any errors and it displays the template within template-preview, but the {{name}} is empty. Any ideas if I'm missing something here?

Thank you!

2012-04-05 22:07
by dzm
Pretty new to Handlebars myself but could it be that it's pulling some cached/precompiled template - Jovan Perovic 2012-04-05 22:22


0

This works for me (tested with Chrome). Which version of handlebars you're using?

<!doctype html>
<head>
</head>
<body>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
     <script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.0.beta.6.js"></script>
     <script id="template" type="text/x-handlebars-template">
     <div>Test Template</div>
     <p>Name: {{name}}</p>
     </script>
     <div id="template-preview"></div>

     <script>
        var source   = $("#template").html();
        var template = Handlebars.compile(source);
        var data = { name : 'Michael'};
        $("#template-preview").html(template(data));
     </script>
</body>
</html>
2012-04-05 22:25
by lowik
Hm, stand alone that works for me too. I'm also using Head.js - maybe it's related to tha - dzm 2012-04-05 22:28
Can you show more code then to see how exactly you're using it - lowik 2012-04-05 22:32
Ah - I think it's because I'm using handlebars within handlebars - Using handlebars in node.js, then in a template there, I have the other code - dzm 2012-04-05 22:38
in hbs file,view engine will replace all the delimiter with spac - XXX 2017-06-23 09:09
Ads