Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one comment in an erb template?

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?

like image 557
randombits Avatar asked Aug 06 '10 18:08

randombits


People also ask

How do I comment in an ERB file?

erb is by definition "embedded ruby", you can embed every ruby code between: <%= and the other: %> , typically all written in one line. In addition, ruby one-line comments start always with # , so the <%=# Comment %> style matches perfectly with both pure-ruby and erb styles for one-line comments.

How do you comment in Ruby on Rails?

The Ruby single-line comment begins with the # character and ends at the end of the line. Any characters from the # character to the end of the line are completely ignored by the Ruby interpreter. The # character doesn't necessarily have to occur at the beginning of the line; it can occur anywhere.

What is an ERB template?

An ERB template looks like a plain-text document interspersed with tags containing Ruby code. When evaluated, this tagged code can modify text in the template. Puppet passes data to templates via special objects and variables, which you can use in the tagged Ruby code to control the templates' output.

What does ERB stand for Ruby?

ERB stands for Embedded Ruby.


2 Answers

=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
%>
like image 129
Thilo Avatar answered Nov 15 '22 22:11

Thilo


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 %>
like image 40
zetetic Avatar answered Nov 15 '22 22:11

zetetic