Lee McAlilly
Lee McAlilly

Reputation: 9316

What does it mean to add a hyphen to the closing tag of a ruby loop <% -%>

I'm a noob at this and can't figure out why the hyphen gets added to something like this:

Not even sure if my jargon in the title of this question is accurate.

Upvotes: 3

Views: 943

Answers (4)

Kasumi
Kasumi

Reputation: 921

adding the "-" will remove the line break for that line

Upvotes: 10

Patrick Klingemann
Patrick Klingemann

Reputation: 9014

It means simply:

Place any text (HTML) that follows <% -%> on the next line in the rendered template.

Upvotes: 2

JZ.
JZ.

Reputation: 21877

The hyphen is not necessary in the above code. Simply adding <% end %> is sufficient to execute the embedded ruby.

The use of hyphen is fully explained here and essentially effects rendered html. In your case, what the hyphen does is this:

1   Hyphen at the end of the tag, just the two spaces
2   before the tag on the line below will be left
3   <% -%>
4   Last line

The code will output with two spaces before " Last Line" just below your <% -%> tag

Upvotes: 1

mikewilliamson
mikewilliamson

Reputation: 24793

It means that it will add the \n (or maybe \r\n, I forget which) to the line. It just effects way the HTML is formatted.

So if did:

>> helper.image_tag "image.jpg"
=> "<img alt=\"Image\" src=\"/images/image.jpg\" />"

it would output something like:

"<img alt=\"Image\" src=\"/images/image.jpg\" />\r\n"

Meaning that your html page would look like:

<image tag>
<whatever other tag>

instead of having them both on the same line.

Upvotes: 0

Related Questions