Eco template renders integer when using 'end' statement

Go To StackoverFlow.com

0

I have a pretty simple Backbone View that I'd like to have render some blocks of HTML conditionally. I'm seeing a behavior where following the conditionally rendered HTML there is a integer rendered as well. From what I can tell, it seems to happen when I use the end statement to terminate a conditional block.

Here's some code that is demonstrating the error for me:

<% if true: %>Hello World!<% end %>

I would expect this to renderHello World! into the containing element. However, it's actually rendering Hello World!2.

If I add several of blocks in the same template:

<% if true: %>Foo, <% end %>
<% if true: %>Bar, <% end %>
<% if true: %>Baz<% end %>

I would expect this to render Foo, Bar, Baz into the containing element. However, it's actually rendering Foo, 2 Bar, 5 Baz8. After running a somewhat larger set of them, it seems the integer being printed goes up by 3 every time. From reading over the gem's README I can't see anything I'm doing wrong.

Any help would be much appreciated!

2012-04-05 20:56
by Drew Miller
Are you passing a variable end with a value to the template? It might be somehow overwriting the default end functionality - abraham 2012-04-06 18:21
In this case the only things that would be in the template scope would be the rendering object (a Backbone View). It's not being passed any additional context other than that.

That being said... maybe end in the context of the Backbone View is a helper function or something? I'll look into that - Drew Miller 2012-04-08 20:20

Couldn't find any root cause down that path. In the end, I switched my app back to JavaScript and the templates to EJS. Not the outcome I was looking for, but at least it works - Drew Miller 2012-04-09 17:34


0

Alternatively you could use the postfix format.

<%= "Hello World!" if true %>
2012-04-09 04:17
by abraham
Yeah, that definitely works for things I can express in a single line like that; but I would, for some cases, need the ability to do blocks. Thanks for the suggestion - Drew Miller 2012-04-09 17:35


0

I have the same problem. Have you solved it already?

I think it's caused by if statements being wrapped in __obj.push() calls in the compiled JS. For example, in my script, there is a piece of code

<%- if !@blok: %>
  <%- to_html(@create_link('c', @session['course'])) %>
<%- end %>

which gets compiled to

[1] __out.push(!this.blok ?
[2]  (__out.push('\n '),
[3]   __out.push(to_html(this.create_link('c', this.session['course']))),
[4]   __out.push('\n ')
[5]  ) : void 0)

If I understand it correctly, it means that it will first push the \n, the result of the function, another \n to the output. But the last push (line 4) will return number of the elements in the array, which will be in turn pushed to the array itself by the outer push (line 1).

I've tried to change the Array.prototype.push to return something else, but it seems that messes things even more (since it's a core function).

2012-08-16 14:06
by phoeniks
Installing Node.js solved the problem - phoeniks 2012-08-16 16:33
How did it solve the problem and whyyyyyy?! Things like these drives me crazy - Henrik Andersson 2014-07-01 06:35
Ads