Comment syntax and strange partial rendering

Using rails 2.3.2 I have a partial _foo.rhtml that begins with a comment as follows:

    <% # here is a comment %>     <li><%= foo %></li>

When I render the partial from a view in the traditional way, e.g.

    <% some_numbers = [1, 2, 3, 4, 5] %>     <ul>     <%= render :partial => "foo", :collection => some_numbers %>     </ul>

I found that the <li> and </li> tags are not rendered. However, I can solve this problem by fixing _foo.rhtml to eliminate the space between <% and # so that it reads:

    <%# here is a comment %>     <li><%= foo %></li>

My question: what's going on here? E.g., is <% # comment %> simply incorrect syntax for including comments in a template? Or is the problem more subtle?

Thanks!

Using rails 2.3.2 I have a partial _foo.rhtml that begins with a comment as follows:

<% # here is a comment %> <li><%= foo %></li>

When I render the partial from a view in the traditional way, e.g.

<% some_numbers = [1, 2, 3, 4, 5] %> <ul> <%= render :partial => "foo", :collection => some_numbers %> </ul>

I found that the <li> and </li> tags are not rendered. However, I can solve this problem by fixing _foo.rhtml to eliminate the space between <% and # so that it reads:

<%# here is a comment %> <li><%= foo %></li>

My question: what's going on here? E.g., is <% # comment %> simply incorrect syntax for including comments in a template? Or is the problem more subtle?

<% # is invalid syntax, the only comment syntax allowed in erb is <%#

Unfortunately using <% # does not necessarily give a syntax error, just unexpected results.

Colin

The result is pretty much expected.

Since you started the general Ruby code block, the closing "%>" is now the part of the comment and doesn't denote the code block end any more. To see the difference, try:

<%   # Comment %>

- Aleksey

Colin Law wrote:

... <% # is invalid syntax, the only comment syntax allowed in erb is <%#

It is not actually invalid. Just the rest of the line is considered as comment including %>. Working (though ugly) code was:

<% # here is a comment %>

T.

It may work but I am not sure it is valid erb syntax. In a previous version of Rails <% # comment %> worked. I am not sure that <% # here is a comment %> can be guaranteed to work in the future. I may be wrong though.

Colin

Colin Law wrote:

It may work but I am not sure it is valid erb syntax. In a previous version of Rails <% # comment %> worked. I am not sure that <% # here is a comment %> can be guaranteed to work in the future. I may be wrong though.

Your probably right. And there really should be at least a warning when it is found in a template.

T. N. T. wrote:

Colin Law wrote:

It may work but I am not sure it is valid erb syntax. In a previous version of Rails <% # comment %> worked. I am not sure that <% # here is a comment %> can be guaranteed to work in the future. I may be wrong though.

Your probably right. And there really should be at least a warning when it is found in a template.

Wow! Add this weirdness to the list of reasons to use Haml rather than ERb (as if I needed more reasons... :slight_smile: ).

Best,

I am with you on that one Marnen. I tried haml a little while ago and was immediately hooked. I would highly recommend anyone who has not tried it to have a go.

Colin

Ah, this makes a lot of sense...thanks everyone!

-Ben

PS If content-free thank-yous isn't appropriate ettiquette, please let me know :slight_smile: