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
displayed.
is this possible? what is the code to embed a layout within a layout?
- 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 %>
...someHTML...
- 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">
blablabla
<%= yield %> (This puts the rendered template of the action that was
requested in here)
blablabla
<div>
<%end%>
<%= 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: