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.