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

foreach logic enhancement #178

Closed
punund opened this issue Jun 9, 2013 · 3 comments
Closed

foreach logic enhancement #178

punund opened this issue Jun 9, 2013 · 3 comments

Comments

@punund
Copy link

punund commented Jun 9, 2013

I suggest the following feature for foreach: alognside with else block which is rendered when collection is empty, there may be:

  • header block, which is executed before iterating over collection
  • trailer block, which is executed after iterating over collection
  • enclose block, rendering around

All of those are run only if the collection is not empty:

foreach users as user
  tr
    td #{user.firstName} #{user.lastName} (#{user.age})
header
  tr
    th User name and age
trailer
  tr
    td You have #{users.length} users today
enclose
  table
else
  p No users were found

I don't know how hard to implement and/or useful this would be, but almost every collection has a custom rendering depending on whether it's empty or not.

@bminer
Copy link
Owner

bminer commented Jun 10, 2013

Interesting feature... I will have to think about this... my initial reaction is that it's almost more confusing syntax than simply adding a - if(users.length > 0) .... else .... block.

@punund
Copy link
Author

punund commented Jun 10, 2013

This may be more confusing. On the other hand, writing

- if (users.length > 0)
  table
    foreach users as user
      tr
        td= user.name
- else
  p No users

versus

foreach users as user
  tr
    td= user.name
enclose
  table
else
  p No users

indents two levels deeper, uses visually bad "non-blade" if, and evaluates collection twice, which is not very DRY and may confuse too 😱

@bminer
Copy link
Owner

bminer commented Jun 21, 2013

I like your idea... just not the syntax proposed, especially the enclose keyword. My opinion only... I think the extra indenting improves readability even though it is, perhaps, more verbose.

I think I will close this issue for now, but if you have any other ideas, I'd certainly welcome them.

@bminer bminer closed this as completed Jun 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants