Forum: Ruby on Rails a master layout in which other layouts are embedded?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Marc n. (Guest)
on 2007-02-20 15:34
(Received via mailing list)
hi all

i would like to design my site so there is a master layout which is
the same for every page or a group of pages then a layout for each
controller, as scaffolds create, which gets embedded within the master
layout. then finally within this layout the actual action code is

is this possible? what is the code to embed a layout within a layout?


Thorsten (Guest)
on 2007-02-20 16:21
(Received via mailing list)
- application.rhtml is the master layout that gets rendered if no
other layout is specified. inside it, you put something like this to
the place where your "subtemplate" of each controller should go in
...some HTML...
<%= yield :sublayout %>
- now you have a controller named "articles", then you create a layout
"articles.rhtml" inside it you do e.g. this:

<%content_for "sublayout" do %>
<div id="sublayout">
<%= yield %> (This puts the rendered template of the action that was
requested in here)
<%= render :file "layouts/application.rhrml%> (this renders the master
layout around it)

1) rails renders the action template (e.g. "show.rhtml"),
2) puts it inside the controller's "articles.rhtml" layout, which is
saved to the content_for "sublayout"
3) "application.rhtml" is rendered, and the whole "articles.rhtml" is
oput inside it where you specified <%= yield :subtemplate %>
You may read this article about best practice for sidebars in rails,
the idea is usable for stuff like this too:
This topic is locked and can not be replied to.