Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18355 > unrolled thread
| Started by | jr <jlrough@yahoo.com> |
|---|---|
| First post | 2013-02-05 15:05 -0800 |
| Last post | 2013-02-07 00:27 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.javascript
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
| From | jr <jlrough@yahoo.com> |
|---|---|
| Date | 2013-02-05 15:05 -0800 |
| Subject | handlebars javascript error on return in function, i is not defined |
| Message-ID | <ab80af3d-ae71-455f-a5de-e9e67a0ba2f9@googlegroups.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>
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-02-06 04:15 +0100 |
| Message-ID | <9385853.fL9pMimAyI@PointedEars.de> |
| In reply to | #18355 |
Not usually reading Googlodytes, but FWIW …
jr wrote:
> I want to print a simple list using a js template in this order:
There is no such thing as a “js template”. There is no single “js” language
to begin with.
> 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">
“text/x-handlebars-template” is not a registered MIME media type. If it
works at all in a user agent then the scripting language used is, at best, a
very distant relative of ECMAScript and therefore very probably off-topic
here.
<http://www.w3.org/TR/2012/CR-
html5-20121217/scripting-1.html#scriptingLanguages>
> […] 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;
In a conforming implementation of ECMAScript without syntax extensions that
would be two /LabelledStatement/s where each evaluates to the value of ”i”
without further effect. In the “text/x-handlebars-template” scripting
language, however, it may mean anything.
> }
>
> return {ret, product:i, benefit:i};
There is no telling (without RTFM, which you should have done) how this
rather obscure scripting language works. For example, it is possible that,
contrary to strict implementations of ECMAScript Ed. 5.x, it uses block
scoping which would cause “i” to be unavailable outside of the loop because
it was declared inside of it. Which explain the ReferenceError exception.
On the other hand, you may have simply made a copy-and-paste error again.
You have a long record on Usenet to be challenged by even the simplest
programming tasks. I wonder, why are you repeatedly and continuously
choosing the most obscure tools (here: *handlebars* *Release Candidate* 1)
to achieve what you want? Why not finally learn this craft from the ground
up, starting with simple standards-compliant code that you have actually
written yourself?
<https://developer.mozilla.org/en-US/docs/>
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | JRough <janis.rough@gmail.com> |
|---|---|
| Date | 2013-02-06 10:24 -0800 |
| Message-ID | <a737d8d1-a76a-47ff-a797-2a8b66cc7a76@googlegroups.com> |
| In reply to | #18355 |
On Tuesday, February 5, 2013 3:05:10 PM UTC-8, jr wrote:
Hello Thomas,
That makes sense, it only needs to be one "I" at a time, it will be the current context whether, benefits or products loop. Is there a way to wrap the i up in the return line so I can grab the count?
Handlebars.registerHelper('each', function(context, options) {
var ret = "";
for(var i=0, j=context.length; i<j; i++) {
ret = ret + options.fn(context[i]);
}
return {ret, i};
});
var h
> 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>
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-02-07 00:27 +0100 |
| Message-ID | <3132599.tVyl27kpNs@PointedEars.de> |
| In reply to | #18369 |
JRough wrote:
> That makes sense, it only needs to be one "I" at a time, it will be the
> current context whether, benefits or products loop. Is there a way to
> wrap the i up in the return line so I can grab the count?
>
> Handlebars.registerHelper('each', function(context, options) {
> var ret = "";
>
> for(var i=0, j=context.length; i<j; i++) {
> ret = ret + options.fn(context[i]);
>
> }
>
> return {ret, i};
> });
> var h
What can I reply to this?
If I tell you despite this being off-topic here, the answer might be wrong
because I do not care to learn the obscure non-ECMAScript language that you
are using. Then again, my answer might be right, whereas you will not have
learned anything from the experience. So you would continue to post such
unreadable off-topic articles via Google Groups.
But if I choose to save my free time for more interesting questions, and do
not tell you anything, you may never know what went wrong here.
So I just reply to you: Mu.
*Think* about what I posted and what you neglected to quote. Then,
hopefully, you will be able to answer the question that you should have
asked instead, by yourself. Even if you will not be able to answer the real
question for yourself, you will know the real question by then, which is a
first step in the right direction.
And (this is for all whom it may concern) *please* read one of the following
carefully before you post to Usenet (again):
<http://jibbering.com/faq/> pp.
<http://PointedEars.de/faq> pp.
HTH
--
PointedEars
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web