Answer the question
In order to leave comments, you need to log in
Why doesn't the code using Handlebars work?
I just started to study this template engine, it seems that there are no special difficulties. But the simplest example I wrote doesn't work. Why?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>DOGE</title>
</head>
<body>
<script type="text/javascript" src="libs/handlebars.js"></script>
<script id="template" type="text/x-handlebars-template">
<h2 class="updates">{{name}}</h2>
</script>
<script type="text/javascript">
var data = {
name : 'John Doe'
},
var template = Handlebars.compile( $('#template').html() );
$('.updates').append( template(data) );
</script>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
You are trying to attach html to an element that does not exist in the DOM.
eg: jsfiddle.net/aybalasubramanian/N2b5M/1
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>DOGE</title>
</head>
<body>
<script type="text/javascript" src="libs/handlebars.js"></script>
<script id="template" type="text/x-handlebars-template">
<h2 >{{name}}</h2>
</script>
<script type="text/javascript">
var source = $("#template").html();
var template = Handlebars.compile(source);
var data = {
name : 'John Doe'
},
$('h2').append(template(data));
</script>
</body>
</html>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question