Issue with named blocks when a partial is used inside of for loops
paulyoung opened this issue · comments
Below is a contrived example that demonstrates my issue.
base
<% content 'before' %>
<p><% content %></p>
<% content 'after' %>
person
<% extend 'base' %>
<% block 'before' : %>
<style type="text/css">
#<%- @id %> {
color: <%- @favoriteColor %>;
}
</style>
<% end %>
<span id="<%- @id %>">Hello <%- @name %>!</span>
<% block 'after' : %>
<script>
var span;
span = document.getElementById('<%- @id %>');
console.log(span.innerText);
</script>
<% end %>
people
<%
people = [
{
name: 'Alice'
favoriteColor: 'red'
}
{
name: 'Bob'
favoriteColor: 'blue'
}
]
%>
<% for person, index in people : %>
<%
options =
id: "person_#{index}"
name: person.name
favoriteColor: person.favoriteColor
%>
<% include 'person', options %>
<% end %>
This produces:
<style type="text/css">
#person_0 {
color: red;
}
</style>
<span id="person_0">Hello Alice!</span>
<script>
var span;
span = document.getElementById('item_0');
console.log(span.innerText);
</script>
<style type="text/css">
#person_0 {
color: red;
}
</style>
<span id="person_1">Hello Bob!</span>
<script>
var span;
span = document.getElementById('item_0');
console.log(span.innerText);
</script>
Reproducable on http://ectjs.com:
The issue seems to stem the tracking of named blocks being somewhat naïve.
Lines 303 to 306 in 0edfa8c
In my example, a block with the same name already exists (because I'm using the partial inside of a loop) and therefore the existing block is used.
This is problem of coffee-script itself, not ECT.
You need to do something similar to this:
for i in [0..10]
do (i) ->
print i
And yes, blocks are not a good idea here.
This is problem of coffee-script itself, not ECT.
You need to do something similar to this:for i in [0..10] do (i) -> print i
I don't think that's correct. As you can see in the example above, the context is fine for unnamed blocks.
...
<span id="person_0">Hello Alice!</span>
...
<span id="person_1">Hello Bob!</span>
...
The issue appears to be that blocks can't be redefined as in #80.
If you can provide an example of how using do
fixes the issue, that would be great.
And yes, blocks are not a good idea here.
What do you mean by that?
Blocks are not good because #80
I'd use a function returning HTML
<% fn (attr) => %>
write something: <%= attr %>
<% end %>
...
<%- fn (attributes...) %>
Thanks for the tip. Seems to be the best option as a workaround for now.