Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with named blocks when a partial is used inside of for loops #83

Open
paulyoung opened this issue Aug 18, 2014 · 8 comments
Open

Comments

@paulyoung
Copy link

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>
@paulyoung
Copy link
Author

Reproducable on http://ectjs.com:

screen shot 2014-08-18 at 17 37 56

@paulyoung
Copy link
Author

The issue seems to stem the tracking of named blocks being somewhat naïve.

ect/lib/ect.js

Lines 303 to 306 in 0edfa8c

TemplateContext.prototype.block = function (name) {
if (!this.blocks[name]) { this.blocks[name] = ''; }
return !this.blocks[name].length;
};

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.

@kuba-kubula
Copy link
Contributor

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

@kuba-kubula
Copy link
Contributor

And yes, blocks are not a good idea here.

@paulyoung
Copy link
Author

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.

@paulyoung
Copy link
Author

And yes, blocks are not a good idea here.

What do you mean by that?

@kuba-kubula
Copy link
Contributor

Blocks are not good because #80
I'd use a function returning HTML

<% fn (attr) => %>
write something: <%= attr %>
<% end %>
...
<%- fn (attributes...) %>

@paulyoung
Copy link
Author

Thanks for the tip. Seems to be the best option as a workaround for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants