# Comment syntax and strange partial rendering

**URL:** <https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777>\
**Category:** Rails\
**Created:** [January 18, 2010, 5:31am UTC](https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777 "2010-01-18T05:31:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![brahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@brahn](https://www.ruby-forum.com/u/brahn)\
**Post date:** [January 18, 2010, 5:31am UTC](https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777/1 "2010-01-18T05:31:57Z")

</div>

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

- and 
 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!

---

<div class="post-metadata">

**Author:** ![brahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@brahn](https://www.ruby-forum.com/u/brahn)\
**Post date:** [January 18, 2010, 9:48am UTC](https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777/2 "2010-01-18T09:48:54Z")

</div>

2010/1/18 Benjamin R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â \<%= render :partial =\> “foo”, :collection =\> some\_numbers %\>  
> 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

---

<div class="post-metadata">

**Author:** ![brahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@brahn](https://www.ruby-forum.com/u/brahn)\
**Post date:** [January 18, 2010, 12:32pm UTC](https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777/3 "2010-01-18T12:32:27Z")

</div>

Colin L. 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.

---

<div class="post-metadata">

**Author:** ![brahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@brahn](https://www.ruby-forum.com/u/brahn)\
**Post date:** [January 18, 2010, 1:08pm UTC](https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777/4 "2010-01-18T13:08:29Z")

</div>

2010/1/18 T.N. T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Colin L. 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  
> %\>

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

---

<div class="post-metadata">

**Author:** ![brahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@brahn](https://www.ruby-forum.com/u/brahn)\
**Post date:** [January 18, 2010, 1:37pm UTC](https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777/5 "2010-01-18T13:37:49Z")

</div>

Colin L. 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.

---

<div class="post-metadata">

**Author:** ![brahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@brahn](https://www.ruby-forum.com/u/brahn)\
**Post date:** [January 18, 2010, 3:52pm UTC](https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777/6 "2010-01-18T15:52:46Z")

</div>

T. N. T. wrote:

> Colin L. 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… 🙂 ).

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![brahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@brahn](https://www.ruby-forum.com/u/brahn)\
**Post date:** [January 18, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777/7 "2010-01-18T16:01:45Z")

</div>

2010/1/18 Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> …
> 
> Wow! Â Add this weirdness to the list of reasons to use Haml rather than  
> ERb (as if I needed more reasons… 🙂 ).

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

---

<div class="post-metadata">

**Author:** ![brahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@brahn](https://www.ruby-forum.com/u/brahn)\
**Post date:** [January 19, 2010, 3:06am UTC](https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777/8 "2010-01-19T03:06:21Z")

</div>

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

-Ben

PS If content-free thank-yous isn’t appropriate ettiquette, please let  
me know 🙂

---

<div class="post-metadata">

**Author:** ![brahn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@brahn](https://www.ruby-forum.com/u/brahn)\
**Post date:** [January 18, 2010, 3:40pm UTC](https://www.ruby-forum.com/t/comment-syntax-and-strange-partial-rendering/181777/9 "2010-01-18T15:40:41Z")

</div>

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
