Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18355
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2013-02-05 15:05 -0800 |
| Message-ID | <ab80af3d-ae71-455f-a5de-e9e67a0ba2f9@googlegroups.com> (permalink) |
| Subject | handlebars javascript error on return in function, i is not defined |
| From | jr <jlrough@yahoo.com> |
I want to print a simple list using a js template in this order:
User, product list, benefit list, status.
I get a reference error on the value returned.
ReferenceError: i is not defined
I can tell what the problem is but I don't know how to solve it.
I need to use the {{#each}}{{/each}} function to count the number of products and benefits.
I just want to add the count of i on each, {{each}} block to the returned options and context .
I am not sure what this line returns but I dont' need the values returned just the count.
ret = ret + options.fn(context[i]);
<html>
<head>
<script type="text/javascript" src="js/vendor/handlebars-1.0.rc.1.js"></script>
<script type="text/x-handlebars-template">
var template =
<div style="display:inline-block">
{{fullname}} +" " +
{{#each products}}
Products +" " + {{products}} +" " +
{{/each}}
{{#each benefits}}
Benefits +" "+ {{benefits}}
{{/each}} +" "+
{{status}} + " " + "Status"
</div>
var context = { user: {{fullname}},
product:[
{product: "product1 "},
{product:"product2"},
{product:"product3 "}
],
benefit :[
{benefit:"benefit1 "},
{benefit:"benefit2 "},
{benefit:"benefit3"}
],
status: "active"
};
Handlebars.registerHelper('each', function(context, options) {
var ret = "";
for(var i=0, j=context.length; i<j; i++) {
ret = ret + options.fn(context[i]);
product:i;
benefit:i;
}
return {ret, product:i, benefit:i};
});
var html = template(context);
console.log.html;
</script>
</head>
<body>
</body>
Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread
handlebars javascript error on return in function, i is not defined jr <jlrough@yahoo.com> - 2013-02-05 15:05 -0800
Re: handlebars javascript error on return in function, i is not defined Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-06 04:15 +0100
Re: handlebars javascript error on return in function, i is not defined JRough <janis.rough@gmail.com> - 2013-02-06 10:24 -0800
Re: handlebars javascript error on return in function, i is not defined Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-07 00:27 +0100
csiph-web