This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 7 years ago.
Could someone explain the difference between using <%= some_ruby_code %> and <% some_other_ruby_code %>
I'm aware that they're not equivalent but can't find a good explanation as to why.
Thanks in advance.
In ERB (Embedded Ruby), using <%= %> will both evaluate and render the evaluated Ruby within your markup, whereas just using <% %> will only evaluate the enclosed expression, and will not print anything to the erb file.
Hope it helps!
In other words, with <%= %> you execute ruby code and print the output. Using <% %> is only necessary to run the ruby code.
To clarify, this is Emebedded Ruby which you use for views pages on Rails. This might helpful for you: https://docs.puppetlabs.com/puppet/latest/reference/lang_template_erb.html
Related
This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 7 years ago.
What are the differences between "<%" and "<%="? How do you know which to use? I'm teaching myself and one of the tutorials I'm using listed the above symbol sets.
You must use <%= %> for instructions whose output you want to display in your view, and use <% %> otherwise.
<%= %> use to output something
<% %> use to add your conditions for example
This question already has answers here:
Difference between "<%=" and "<%" when mixing ruby with html?
(2 answers)
Closed 7 years ago.
I've been working through some ruby on rails tutorials, and I see that the <% %> and <%= %> symbols are used for inline ruby, but what is the difference between those two pairs of symbols?
They are generally used in embedded ruby (erb) files.
<% %> is to execute the Ruby code within brackets, <%= %> is to print output in the file.
This question already has answers here:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
(7 answers)
Closed 8 years ago.
<%= -%>
<%= %>
I am confused about this. Anyone can tell me details? Thank you in advance.
<%= %> prints something into ERB template
<%= -%> prints something into ERB template and avoids line break after expression.
Take a look here for details Why many people use "-%>" instead of "%>" in Rails?
This question already has an answer here:
embedded ruby syntax "<%= .. %>" in rails [duplicate]
(1 answer)
Closed 8 years ago.
I've been following Agile Web Development with Rails, and their code suddenly switches from the use of <%= %> to <% %>.
Code Example 1:
<%= 1+2 %>
Code Example 2:
<% for file in #files %>
file name is: <%= file %>
<% end %>
My question is, for the second code sample why can I not use <%= %>?
Thanks!
In ERB (embedded ruby) syntax, <%= %> is shorthand for "perform the following ruby code AND THEN print the result". So in the first example, it will print the result of the operation 1 + 2, 3.
The second example shows a for loop which will iterate and print the contents between the for declaration and its corresponding <% end %>.
To answer your question, the expression for file in #files itself doesn't return anything worth printing so there's no need to use <%= %> and in fact doing so can cause a hard-to-track-down bug.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between -%> and %> in rails
So for an ERB file, what's the difference between <%- ... -%> vs. <% ... %>? As far as I can tell, they serve the same purpose but one requires more typing than the other.
Using the dash removes the whitespace around the ERB tag, there is a RailsCast that talks about this and some other View tricks.