What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
Rails does not use the stdlib's ERB by default, it uses erubis. Sources: this dev's comment, ActionView's gemspec, accepted merge request I did while writing this.
There are behavior differences between them, in particular on how the hyphen operators %-
and -%
work.
Documentation is scarce, Where is Ruby's ERB format "officially" defined? so what follows are empirical conclusions.
All tests suppose:
require 'erb'
require 'erubis'
When you can use -
- ERB: you must pass
-
totrim_mode
option ofERB.new
to use it. - erubis: enabled by default.
Examples:
begin ERB.new("<%= 'a' -%>\nb").result; rescue SyntaxError ; else raise; end
ERB.new("<%= 'a' -%>\nb" , nil, '-') .result == 'ab' or raise
Erubis::Eruby.new("<%= 'a' -%> \n b").result == 'a b' or raise
What -%
does:
ERB: remove the next character if it is a newline.
erubis:
in
<% %>
(without=
),-
is useless because<% %>
and<% -%>
are the same.<% %>
removes the current line if it only contains whitespaces, and does nothing otherwise.in
<%= -%>
(with=
):- remove the entire line if it only contains whitespaces
- else, if there is a non-space before the tag, and only whitesapces after, remove the whitespces that come after
- else, there is a non-space after the tag: do nothing
Examples:
# Remove
ERB.new("a \nb <% 0 -%>\n c", nil, '-').result == "a \nb c" or raise
# Don't do anything: not followed by newline, but by space:
ERB.new("a\n<% 0 -%> \nc", nil, '-').result == "a\nb \nc" or raise
# Remove the current line because only whitesapaces:
Erubis::Eruby.new(" <% 0 %> \nb").result == 'b' or raise
# Same as above, thus useless because longer.
Erubis::Eruby.new(" <% 0 -%> \nb").result == 'b' or raise
# Don't do anything because line not empty.
Erubis::Eruby.new("a <% 0 %> \nb").result == "a \nb" or raise
Erubis::Eruby.new(" <% 0 %> a\nb").result == " a\nb" or raise
Erubis::Eruby.new(" <% 0 -%> a\nb").result == " a\nb" or raise
# Don't remove the current line because of `=`:
Erubis::Eruby.new(" <%= 0 %> \nb").result == " 0 \nb" or raise
# Remove the current line even with `=`:
Erubis::Eruby.new(" <%= 0 -%> \nb").result == " 0b" or raise
# Remove forward only because of `-` and non space before:
Erubis::Eruby.new("a <%= 0 -%> \nb").result == "a 0b" or raise
# Don't do anything because non-whitespace forward:
Erubis::Eruby.new(" <%= 0 -%> a\nb").result == " 0 a\nb" or raise
What %-
does:
ERB: remove whitespaces before tag and after previous newlines, but only if there are only whitespaces before.
erubis: useless because
<%- %>
is the same as<% %>
(without=
), and this cannot be used with=
which is the only case where-%
can be useful. So never use this.
Examples:
# Remove
ERB.new("a \n <%- 0 %> b\n c", nil, '-').result == "a \n b\n c" or raise
# b is not whitespace: do nothing:
ERB.new("a \nb <%- 0 %> c\n d", nil, '-').result == "a \nb c\n d" or raise
What %-
and -%
do together
The exact combination of both effects separately.
<% %>
Executes the ruby code within the brackets.
<%= %>
Prints something into erb file.
<%== %>
Equivalent to <%= raw %>
. Prints something verbatim (i.e. w/o escaping) into erb file. (Taken from Ruby on Rails Guides.)
<% -%>
Avoids line break after expression.
<%# %>
Comments out code within brackets; not sent to client (as opposed to HTML comments).
Visit Ruby Doc for more infos about ERB.
<% %>
and <%- and -%>
are for any Ruby code, but doesn't output the results (e.g. if statements). the two are the same.
<%= %>
is for outputting the results of Ruby code
<%# %>
is an ERB comment
Here's a good guide: http://api.rubyonrails.org/classes/ActionView/Base.html