S
S
Sergey Pavlov2014-06-26 19:50:48
JavaScript
Sergey Pavlov, 2014-06-26 19:50:48

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

3 answer(s)
R
Roman Zhak, 2014-06-26
@romanzhak

You are trying to attach html to an element that does not exist in the DOM.
eg: jsfiddle.net/aybalasubramanian/N2b5M/1

S
Sergey Pavlov, 2014-06-26
@Pavlov_dog

<!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>

N
newbie78, 2015-02-27
@newbie78

I used Roman 's fiddle
here for example how to work with Handlebars in Mootools
jsfiddle.net/fygm6xv5

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question