# Using Application.rhtml as layout for all controllers

**URL:** <https://www.ruby-forum.com/t/using-application-rhtml-as-layout-for-all-controllers/99050>\
**Category:** Rails\
**Created:** [July 20, 2006, 1:10am UTC](https://www.ruby-forum.com/t/using-application-rhtml-as-layout-for-all-controllers/99050 "2006-07-20T01:10:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![blewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blewis](https://www.ruby-forum.com/u/blewis)\
**Post date:** [July 20, 2006, 1:10am UTC](https://www.ruby-forum.com/t/using-application-rhtml-as-layout-for-all-controllers/99050/1 "2006-07-20T01:10:07Z")

</div>

I put my nav system in application layout. that way any controller  
loading will have the nav system. I have one problem though - the \<%=  
yield %\> is in the body, so how would my controllers specify other css  
files?

my application layout would look like this:

title css include javascript include nav system \<%=yield%\>

so that means any controllers go into the body immediatly - so can they  
not have their own css files without specifying them in application  
layout?

Thanks,  
Ben L.

---

<div class="post-metadata">

**Author:** ![blewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blewis](https://www.ruby-forum.com/u/blewis)\
**Post date:** [July 20, 2006, 1:18am UTC](https://www.ruby-forum.com/t/using-application-rhtml-as-layout-for-all-controllers/99050/2 "2006-07-20T01:18:54Z")

</div>

On 7/19/06, Ben L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I put my nav system in application layout. that way any controller  
> loading will have the nav system. I have one problem though - the \<%=  
> yield %\> is in the body, so how would my controllers specify other css  
> files?

Check out the Cascading Stylesheets Plugin:  
[http://www.redhillconsulting.com.au/rails\_plugins.html#cascading\_stylesheets](http://www.redhillconsulting.com.au/rails_plugins.html#cascading_stylesheets)

It will let you have a seperate style sheet for application →   
controller →   
action

Josh

---

<div class="post-metadata">

**Author:** ![blewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blewis](https://www.ruby-forum.com/u/blewis)\
**Post date:** [July 20, 2006, 1:45am UTC](https://www.ruby-forum.com/t/using-application-rhtml-as-layout-for-all-controllers/99050/3 "2006-07-20T01:45:38Z")

</div>

Ripped off from a post yesterday (Credit to Richard L.), this may  
help…

Take a look at:

[http://api.rubyonrails.com/classes/ActionView/Helpers/CaptureHelper.html](http://api.rubyonrails.com/classes/ActionView/Helpers/CaptureHelper.html)

Specifically:

In your layout:

... stuff \<%= @content\_for\_head %\>

In your template:

\<% content\_for(“head”) do %\>  
the javascript / include js file  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![blewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blewis](https://www.ruby-forum.com/u/blewis)\
**Post date:** [July 20, 2006, 1:31am UTC](https://www.ruby-forum.com/t/using-application-rhtml-as-layout-for-all-controllers/99050/4 "2006-07-20T01:31:28Z")

</div>

Josh K. wrote:

> On 7/19/06, Ben L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I put my nav system in application layout. that way any controller  
> > loading will have the nav system. I have one problem though - the \<%=  
> > yield %\> is in the body, so how would my controllers specify other css  
> > files?
> 
> Check out the Cascading Stylesheets Plugin:  
> [http://www.redhillconsulting.com.au/rails\_plugins.html#cascading\_stylesheets](http://www.redhillconsulting.com.au/rails_plugins.html#cascading_stylesheets)
> 
> It will let you have a seperate style sheet for application →   
> controller →   
> action
> 
> Josh

awesome thanks Josh!

-Ben L.

---

<div class="post-metadata">

**Author:** ![blewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blewis](https://www.ruby-forum.com/u/blewis)\
**Post date:** [July 20, 2006, 1:45am UTC](https://www.ruby-forum.com/t/using-application-rhtml-as-layout-for-all-controllers/99050/5 "2006-07-20T01:45:48Z")

</div>

Hello Ben,

> I put my nav system in application layout. that way any controller  
> loading will have the nav system. I have one problem though - the \<%=  
> yield %\> is in the body, so how would my controllers specify other css  
> files?

You can specify parts of code that will be used in layout, like with  
yield /  
@content\_for\_layout system.

yield :foo is the same as @content\_for\_foo, but the first syntax  
is now preferred.

> my application layout would look like this:
> 
> title css include javascript include

here put a \<%= yield :specific\_css %\> for example.

> nav system \<%=yield%\>

And in your view :

\<% content\_for :specific\_css do -%\>  
\<%= stylesheet\_link\_tag “foo” %\>  
other code if you want…  
\<% end -%\>

See  
[http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#M000528](http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#M000528)  
for further information.

> so that means any controllers go into the body immediatly - so can they  
> not have their own css files without specifying them in application  
> layout?

– Jean-FranÃ§ois.
