gmosx
I read this post on Signal vs Noise with great interest. Ryan correctly identifies the problems with local variable declaration in Erb templates and offers a clever Ruby hack as a solution:

<% message.comments.last.tap do |comment| %>
    <li><%= comment.creator %></li>
<% end %>

While .tap() introduces a scope it still smells as bad style. A more fundamental flaw lies underneath: the lack of true separation of data and presentation. Consider the following alternative using Normal Templates:

{:reduce comments}
    <li>{=creator}</li>
{/:reduce}

or the equivalent shorter version:

{:r comments}
    <li>{=creator}</li>
{/:r}

Scoped, safer, shorter and more readable. Do yourself and favor and switch to a push-style templating engine like Normal Template.

0 Comments