All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is the difference between <%, <%=, <%# and -%> in ERB templates (Ruby on Rails)?

This content has been automatically translated from Ukrainian.
In ERB (Embedded Ruby, *.erb files) templates used in Ruby on Rails, there are tags that define different types of embedded code: 
<%
<%=
<%#
-%>
They have the following meanings:
  • <% - this tag is used to insert Ruby code without outputting the result. It is used for logic and conditional expressions. For example, you can use it to create a loop or a conditional statement without directly outputting the result.
  • <%= - this tag is used to insert Ruby code with outputting the result. It is used to insert the value of a variable or the result of an expression directly into the output HTML code. For example, you can use it to display the value of a variable on the page.
  • <%# - this tag is used to ignore Ruby code (ERB comment) during the execution of the template. Everything between <%# and %> will be ignored and not outputted in the resulting HTML code. This is useful when you want to leave comments or temporarily disable a certain part of the code (during debugging).
For example:
<% if condition %>
  <%= variable %>
<% else %>
  <%# commented part %>
<% end -%>
  • -%> - This tag is used to control the whitespace after the closing tag. It is used to remove extra spaces or newline characters that are usually added after the tag %>
For example:
 <% 1 + 1 -%>
will output `2` without adding a space or newline character after it.
Tips:
<% %>  : Executes Ruby code
<%= %> : Outputs the given value in the ERB template
<% -%> : Removes spaces or newline characters after the expression
<%# %> : Commenting code

This post doesn't have any additions from the author yet.

03 May 18:48

How to get a random logical value true or false in Ruby?

meme code
meme code@memecode
06 May 09:19

How to disable File Upload in the Trix editor?

meme code
meme code@memecode
10 May 17:40

How to use hex color codes in CSS with alpha values?

meme code
meme code@memecode
16 May 20:02

What is Origin in Git?

meme code
meme code@memecode
16 May 22:17

How to remove the space between inline and inline-block elements?

meme code
meme code@memecode
17 May 18:52

What is a loop in Javascript? How do for and while loops work in Javascript?

meme code
meme code@memecode
23 May 06:57

What is debugging?

meme code
meme code@memecode
23 May 07:41

What are attr_accessor, attr_reader, and attr_writer in Ruby? What are they used for?

meme code
meme code@memecode
23 May 11:16

How does the has_many through association (many to many) work in Ruby on Rails?

meme code
meme code@memecode
24 May 18:53

What are joins in Ruby on Rails and how does it work?

meme code
meme code@memecode
02 Jun 06:42

Error adding people to Google Family. Failed to load the page.

meme code
meme code@memecode
02 Jun 12:53

What does super do in Ruby?

meme code
meme code@memecode