Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 759
Karsten S. Avatar asked Oct 22 '22 07:10

Karsten S.


1 Answers

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!")

like image 119
CDub Avatar answered Nov 08 '22 21:11

CDub