Difference Between ≪%, ≪%=, ≪%# and -%≫ in Erb in Rails

Difference between -% and % in rails

The extra dash makes ERB not output the newline after the closing tag. There's no difference in your example, but if you have something like this:

<div>
<% if true -%>
Hi
<% end -%>
</div>

It'll produce:

<div>
Hi
</div>

and not this:

<div>

Hi

</div>

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

<% %>

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.

What is the difference between %= ... % and % ... % in Ruby on Rails

This is ERB templating markup (one of many templating languages supported by Rails). This markup:

<% ... %>

is used to evaluate a Ruby expression. Nothing is done with the result of that expression, however. By contrast, the markup:

<%= ... %>

does the same thing (runs whatever Ruby code is inside there) but it calls to_s on the result and replaces the markup with the resulting string.

In short:

<% just run code %>
<%= run code and output the result %>

For example:

<% unless @items.empty? %>
<ul>
<% @items.each do |item| %>
<li><%= item.name %></li>
<% end %>
</ul>
<% end %>

In contrast, here's some Haml markup equivalent to the above:

- unless @items.empty?
%ul
- @items.each do |item|
%li= item.name

Difference between % ... % and %= .. % in rails 3

From the Rails docs, concat is only supposed to be used within a <% %> code block. When you use it in a <%= %> code block, you see it twice because concat appends the provided text to the output buffer, but then it also returns the entire output buffer back to your helper method, which is then output by the <%=, causing your entire page to be duplicated.

Normally, you should not need to use concat much if at all (I've never come across a situation where I needed it). In your helper, you can just do this:

def test
"Hello world"
end

And then use <%= test %> in your view.

The difference between %=, % and %- in ejs

The following is from ejs docs (tag section):

  • <% 'Scriptlet' tag, for control-flow, no output
  • <%= Outputs the value into the template (HTML escaped)
  • <%- Outputs the unescaped value into the template

See the difference between escaped and unescaped html here

Rails ERb best practices (% % vs % -% vs %- -%)

It's a personal preference. I use <% %> when I'm writing a loop or a block, because I want new lines there. I use <% -%> in rare cases of variable assignment. And I never use <%- -%> because that's one option too many.

What is the difference between :to and = in rails

In context of Rails routes:

  • Is there a difference between these two statements?

There is no difference.

  • If so why is one better than the other?

No, it's the same.

  • Why is the rails community switching to the ":" notation (or are
    they)?

Just a more readable, 'from' => 'to' and 'from', to: 'to'

  • Moving forward with rails 4 and soon 5, are both formats still
    acceptable?

Yes.

The => notation it's a hash ruby feature, and related to the :symbol.
You can write symbols by two ways :key => value and key: value.

Rails ERB %- ... -% vs. % ... %

Using the dash removes the whitespace around the ERB tag, there is a RailsCast that talks about this and some other View tricks.

Difference between % % and %= % in RoR

Think of it like this:

<% execute this code and display nothing %> 

and

<%= execute this code and display the result in the view %> 

So, for example you could do this:

<% @values = ['eenie', 'menie', 'miney', 'mo' ] %>
<% @values.each do |value| %>
The current value is <%= value %>!
<% end %>

Why many people use -% instead of % in Rails?

I would like to add some resources that I know about ERB :

  • Rails extends ERB, so that you can suppress the newline simply by adding a trailing hyphen to tags in Rails templates:

    <ul>
    <% for @item in @items -%>
    <li><%= @item %></li>
    <% end -%>
    </ul>
  • Comment markers use a hash sign:

     <%# This is just a comment %>
  • A tag with an equals sign indicates that enclosed code is an expression, and that the renderer should substitute the code element with the result of the code (as a string) when it renders the template. Use an expression to embed a line of code into the template, or to display the contents of a variable:

     Hello, <%= @name %>.
    Today is <%= Time.now.strftime('%A') %>.
  • With one equal sign the string will be encoded. To avoid encoding, you can use two equals signs (or raw):

        Hello, <%== @unencodedOutput %>
  • Tags without the equals sign denote that the enclosed code is a scriptlet. Each scriptlet is caught and executed, and the final result of the code is then injected in to the output at the point of the scriptlet.

    <ul>
    <% for @item in @shopping_list %>
    <li><%= @item %></li>
    <% end %>
    </ul>

    Scriptlets are most commonly used for embedding loops or conditional logic into templates:

Read An Introduction to ERB Templating to know more.



Related Topics



Leave a reply



Submit