Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between -%> and %> in rails [duplicate]

I have started some rails tutorials and noticed that some of the view code blocks are like

<h1><%= @subject.name -%></h1>

and other code blocks are like

<h1><%= @subject.name %></h1>

What is the difference between -%> and %>

If you know of some good syntax references you can point me to, that would also be helpful.

like image 837
Brettski Avatar asked Jun 15 '09 23:06

Brettski


3 Answers

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> 
like image 178
zenazn Avatar answered Sep 21 '22 21:09

zenazn


I'm pretty sure - before %> is no longer necessary, and should be left out.

At least in Chrome, the generated html looks the same using -%> or %>.

like image 38
gylaz Avatar answered Sep 21 '22 21:09

gylaz


If you use HAML rather than ERB you can do something similar with a less than or greater symbol than after your tag.

> will remove any whitespace around your tag and < will remove any whitespace within it.

.float-left<
  %p
    Lorem ipsum dolor sit amet

is compiled to:

<div class="float-left"><p>
  Lorem ipsum dolor sit amet
</p></div>

And…

%left_tag
%inside>
%right_tag

is compiled to:

<left_tag /><inside /><right_tag />

If you're not using HAML it's definitely worth checking out.

like image 24
James Conroy-Finn Avatar answered Sep 18 '22 21:09

James Conroy-Finn