Reputation: 55
I'm hacking around Handlebars for the first time; my goal is to display a list of items in the html.
This is the script inside the html:
<div>
<script id="list-items" type="text/x-handlebars-template">
{{#each list}} {{ price }} {{/each}}
</script>
</div>
This is the js file:
var list = [
{
name: 'Almond Toe Court Shoes, PatentBlack',
category: 'Women\'s footwear',
price: 42,
quantity: 5
},
{
name: 'Bla bla bla',
category: 'Women\'s footwear',
price: 66,
quantity: 5
}
];
var theTemplateScript = $("#list-items").html();
var theTemplate = Handlebars.compile (theTemplateScript);
$(document.body).append (theTemplate (list));
My code doesn't work and I see no errors in the console. What am I doing wrong?
Upvotes: 3
Views: 66
Reputation: 33344
Your template iterates over a list
attribute but you pass the raw array.
To reconcile your template and the data you pass, either pass an object with a list
attribute :
$(document.body).append (theTemplate ({
list: list
}));
http://jsfiddle.net/nikoshr/hm6psg9w/1/
or modify your template to iterate over an array:
<script id="list-items" type="text/x-handlebars-template">
{{#each .}} {{ price }} {{/each}}
</script>
http://jsfiddle.net/nikoshr/hm6psg9w/
Upvotes: 4