ChatGPT解决这个技术问题 Extra ChatGPT

What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

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

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

what's the usage of each one ?

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

N
Nil
<% %>

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.


i have met somewhere in code this variant else <%== %> does anyone know what is it?
The double equal means that the string is not escaped, as in raw.
Rails does not use the stdlib ERB: it uses erubis: github.com/rails/rails/issues/16766#issuecomment-54148778
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?
@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).
Y
Yeonho

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


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.
@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 %>."
How to suppress leading whitespace for <%= %> ?
C
Community

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.


On the second line you have written that Rails uses eruby while linking to erubis.
@DanielJonsson thanks for the report: it was meant to be erubis. Going nuts with so many versions. Updated.
c
chris

<% %> : Executes the ruby code

<%= %> : Prints into Erb file. Or browser

<% -%> : Avoids line break after expression.

<%# %> : ERB comment


b
bkunzi01

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


this link answers the OP accurately
Link is now broken
Updated with the new link!
H
HeadAndTail

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

A
Aastha Kesarwani

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