Can some one please describe the usage of the following characters which is used in ERB file:
<% %>
<%= %>
<% -%>
<%# %>
what's the usage of each one ?
Can some one please describe the usage of the following characters which is used in ERB file:
<% %>
<%= %>
<% -%>
<%# %>
what's the usage of each one ?
I've added the
<%%
literal tag delimiter as an answer to this because of its obscurity. This will tell erb not to interpret the<%
part of the tag which is necessary for js apps like displaying chart.js tooltips etc.Everything about ERB can be found here: https://docs.puppet.com/puppet/latest/reference/lang_template_erb.html#tags
These are use in ruby on rails :-
<% %> :-
The <% %> tags are used to execute Ruby code that does not return anything, such as conditions, loops or blocks. Eg :-
<%= %> :-
use to display the content .
<% -%>:-
Rails extends ERB, so that you can suppress the newline simply by adding a trailing hyphen to tags in Rails templates
<%# %>:-
comment out the code
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:
When you can use
-
-
totrim_mode
option ofERB.new
to use it.Examples:
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=
):Examples:
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:
What
%-
and-%
do togetherThe exact combination of both effects separately.
<% %>
executes the code in there but does not print the result, for eg:We can use it for if else in an erb file.
Will print
temp is 1
<%= %>
executes the code and also prints the output, for eg:We can print the value of a rails variable.
Will print
1
<% -%>
It makes no difference as it does not print anything,-%>
only makes sense with<%= -%>
, this will avoid a new line.<%# %>
will comment out the code written within this.Executes the ruby code within the brackets.
Prints something into erb file.
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 commentHere's a good guide: http://api.rubyonrails.org/classes/ActionView/Base.html