Karsten S.
Karsten S.

Reputation: 2391

How to avoid newlines being added by HAML ruby block evaluation

I'm using the content_for helper to e.g. set a page title in my rails 3 app. One snippet of erb I'm still using in some places is

<title><%= yield(:title) -></title>

or written in HAML would be

%title= yield(:title)

Now I have a HAML template setting this title:

- content_for :title do
  My awesome title
- content_for :something_else do

which results in

<title>My awesome title
</title>

How can I avoid the newline before </title>? It's definitely set in :title – adding +"X" will put the X behind the newline.

I already tried

-< content_for :title do
-> content_for :title do

but either one leads to an error. Whitespace removal seems to be not working with ruby evaluation.

Upvotes: 5

Views: 1065

Answers (1)

CDub
CDub

Reputation: 13344

In your layout template, do:

%title= content_for?(:title) ? "#{yield(:title)}" : "Default Title"

Then in your content templates, do:

= content_for(:title, "Welcome to my home page!")

Upvotes: 3

Related Questions