12

I have some trivial markup that looks like the following:

<li class="someclass">
  <=% t'model.attr' %>
</li>

Is there a trivial way to comment that out? Just wrapping <!-- --> around the block will still leave the ruby code available to the template. This means I have to comment out the HTML and Ruby specific code separately.

What's the best way to comment out all three lines with the least amount of markup?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
randombits
  • 47,058
  • 76
  • 251
  • 433

4 Answers4

12

=begin and =end are the Ruby version of block comments.

Using them in an erb template:

<%
=begin
%>
<li class="someclass">
  <=% t'model.attr' %>
</li>
<%
=end
%>
Jono
  • 3,949
  • 4
  • 28
  • 48
Thilo
  • 17,565
  • 5
  • 68
  • 84
  • This does work, and it's the best way I've found, but doesn't it mean something semantically besides "comment this out?" I wish there were a more sanctioned way, if so. – Nathan Long Aug 06 '10 at 18:40
  • 3
    `=begin` and `=end` are the Ruby version of block comments. Alternatively, you could also do `<<-ASDF` and `ASDF` (replacing ASDF with whatever keyword you want). This is the heredoc comment format. – Karl Aug 06 '10 at 22:26
11

You can comment ERB blocks using #:

<!-- <li class="someclass"> -->
  <%#= t'model.attr' %>
<!-- </li> -->

or avoid the literal HTML using Rails content_tag method:

<%#= content_tag :li, t'model.attr', :class=>:someclass %>
zetetic
  • 47,184
  • 10
  • 111
  • 119
4

Doesn't work:

<%# <li class="someclass">
  <=% t'model.attr' %>
</li> %>

Does work:

<% if false %>
<li class="someclass">
  <=% t'model.attr' %>
</li>    
<% end %>
Daniel O'Hara
  • 13,307
  • 3
  • 46
  • 68
  • Coming over from Python/etc this syntax is easy to use and would be easier to read for a non Ruby person. A win in my book any day. – Marc Apr 06 '14 at 21:01
0

Edited because I noticed the true intention of your question:

<%
=begin
%>
<li class="someclass">
  <%= t'model.attr' %>
</li>
<%
=end
%>

In every syntax highlighter that I've used (mainly textmate), this needs to be at the very beginning of the line, you can't indent it for it to appear commented. I don't know if that's a rule or a poor implementation of the highlighting.

theIV
  • 25,434
  • 5
  • 54
  • 58