Can some one please describe the usage of the following characters which is used in ERB file:

<%   %>
<%=  %>
<%  -%>
<%#  %>

what's the usage of each one ?

upvote
  flag
You've got some great answers here. Also note that Haml is often a superior alternative to ERb. – Marnen Laibow-Koser
1 upvote
  flag
Yes, it is much easier to deal with than ERb – simo
1 upvote
  flag
upvote
  flag
@Ciro Santilli: Despite the age, this one seems more complete than your references. I'm voting to close those as dupes of this one.. – Brad Werth
upvote
  flag
<%= %> and <%= -%> are different: only the latter removes trailing whitespaces. – illusionist

5 Answers 11

up vote 362 down vote accepted
<% %>

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.

8 upvote
  flag
i have met somewhere in code this variant else <%== %> does anyone know what is it? – okliv
22 upvote
  flag
The double equal means that the string is not escaped, as in raw. – Richard_G
5 upvote
  flag
1 upvote
  flag
But... but... <%# 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? – BalinKingOfMoria
1 upvote
  flag
@BalinKingOfMoria I'm fairly certain it's treated specially -- as in, instead of the comment being read and processed, like it would be if it was just a Ruby comment, it's skipped entirely. The effect is the same, though. – Nic Hartley
5 upvote
  flag
@QPaysTaxes sorry about that, you're right... For example, if you have multiline ERB <%...%>, <%#...%> 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). – BalinKingOfMoria
upvote
  flag
DONT FORGET '<%%' If anyone has used Chart.js in an erb template they've needed to do this for tooltips lol the <%% basically means dont interpret the '<%' . – bkunzi01

<% %> 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

upvote
  flag
I don't see that opening <%- 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. – Andrew Vit
1 upvote
  flag
@AndrewVit - From the page referenced in this answer (ActionView::Base docs page): "<%- and -%> suppress leading and trailing whitespace, including the trailing newline, and can be used interchangeably with <% and %>." – Earl Jenkins
4 upvote
  flag
How to suppress leading whitespace for <%= %> ? – Bogdan Gusiev

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

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.

1 upvote
  flag
On the second line you have written that Rails uses eruby while linking to erubis. – Daniel Jonsson
1 upvote
  flag
@DanielJonsson thanks for the report: it was meant to be erubis. Going nuts with so many versions. Updated. – Ciro Santilli 华涌低端人口 六四事件 法轮功

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

1 upvote
  flag
this link answers the OP accurately – Ben Tinsley

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" %>

Not the answer you're looking for? Browse other questions tagged or ask your own question.