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.
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>
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 %>
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With