Can some one please describe the usage of the following characters which is used in ERB file:
<% %>
<%= %>
<% -%>
<%# %>
what's the usage of each one ?
-%>
, and stackoverflow.com/questions/3952403/… for <%=
, stackoverflow.com/questions/3901619/… for <%#
.
<%= %>
and <%= -%>
are different: only the latter removes trailing whitespaces.
<% %>
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
<%-
does anything for leading whitespace, and I don't see it documented anywhere. Are you sure about that? Maybe it's a different flavour of ERB. There's no option shown for it on the stdlib docs.
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 - to trim_mode option of ERB.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
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
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
<%= %> : Prints into Erb file. Or browser
<% -%> : Avoids line break after expression.
<%# %> : ERB comment
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.
Update (Fixed broken link)
Everything about ERB can now be found here: https://puppet.com/docs/puppet/5.3/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 :-
<h1>Names of all the people</h1>
<% @people.each do |person| %>
Name: <%= person.name %><br>
<% end %>
<%= %> :-
use to display the content .
Name: <%= person.name %><br>
<% -%>:-
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
<%# WRONG %>
Hi, Mr. <% puts "Frodo" %>
<% %>
executes the code in there but does not print the result, for eg:
We can use it for if else in an erb file.
<% temp = 1 %>
<% if temp == 1%>
temp is 1
<% else %>
temp is not 1
<%end%>
Will print temp is 1
<%= %>
executes the code and also prints the output, for eg:
We can print the value of a rails variable.
<% temp = 1 %>
<%= temp %>
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.
Success story sharing
<%#
is just a normal<%
with a Ruby comment#
right after it.... Yeah, it's fun to think it's something special, but isn't it just an ERB idiom rather than a special syntactic construct?<%...%>
,<%#...%>
comments out all of it, which it wouldn't do if it was just a normal comment (i.e. it would only comment out the line it's on).