diff options
author | Mike Gunderloy <MikeG1@larkfarm.com> | 2008-12-27 09:05:37 -0600 |
---|---|---|
committer | Mike Gunderloy <MikeG1@larkfarm.com> | 2008-12-27 09:05:37 -0600 |
commit | 6d0639c95e5385d65005f58ea37f337455753ea9 (patch) | |
tree | 8c4322640e52588848c3d36b406edcc22a29fd4d /railties/doc/guides/source/layouts_and_rendering.txt | |
parent | e51b8e2036720d49372275bea64c99e6192075fb (diff) | |
download | rails-6d0639c95e5385d65005f58ea37f337455753ea9.tar.gz rails-6d0639c95e5385d65005f58ea37f337455753ea9.tar.bz2 rails-6d0639c95e5385d65005f58ea37f337455753ea9.zip |
Merge patch on subtemplates into layouts & rendering guide
Diffstat (limited to 'railties/doc/guides/source/layouts_and_rendering.txt')
-rw-r--r-- | railties/doc/guides/source/layouts_and_rendering.txt | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/railties/doc/guides/source/layouts_and_rendering.txt b/railties/doc/guides/source/layouts_and_rendering.txt index 63c39ea57a..6811849882 100644 --- a/railties/doc/guides/source/layouts_and_rendering.txt +++ b/railties/doc/guides/source/layouts_and_rendering.txt @@ -6,6 +6,7 @@ This guide covers the basic layout features of Action Controller and Action View * Use the various rendering methods built in to Rails * Create layouts with multiple content sections * Use partials to DRY up your views +* Use nested layouts (sub-templates) == Overview: How the Pieces Fit Together @@ -976,10 +977,58 @@ Rails determines the name of the partial to use by looking at the model name in In this case, Rails will use the customer or employee partials as appropriate for each member of the collection. +=== Using Nested Layouts + +You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller. Rather than repeating the main layout and editing it, you can accomplish this by using nested layouts (sometimes called sub-templates). Here's an example: + +Suppose you have the follow ApplicationController layout: + ++app/views/layouts/application.erb+ + +[source, html] +------------------------------------------------------- +<html> +<head> + <title><%= @page_title %><title> + <% stylesheet_tag 'layout' %> + <style type="text/css"><%= yield :stylesheets %></style> +<head> +<body> + <div id="top_menu">Top menu items here</div> + <div id="menu">Menu items here</div> + <div id="main"><%= yield %></div> +</body> +</html> +------------------------------------------------------- + +On pages generated by NewsController, you want to hide the top menu and add a right menu: + ++app/views/layouts/news.erb+ + +[source, html] +------------------------------------------------------- +<% content_for :stylesheets do %> + #top_menu {display: none} + #right_menu {float: right; background-color: yellow; color: black} +<% end -%> +<% content_for :main %> + <div id="right_menu">Right menu items here</div> + <%= yield %> + <% end -%> +<% render :file => 'layouts/application' %> +------------------------------------------------------- + +NOTE: In versions of Rails before Rails 2.3, you should use +render \'layouts/applications\'+ instead of +render :file => \'layouts/applications\'+ + +That's it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div. + +There are several ways of getting similar results with differents sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the +ActionView::render+ method via "+render \'layouts/news\'+" to base a new layout on the News layout. + == Changelog == http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15[Lighthouse ticket] +* December 27, 2007: Merge patch from Rodrigo Rosenfeld Rosas covering subtemplates * December 27, 2008: Information on new rendering defaults by link:../authors.html#mgunderloy[Mike Gunderloy] * November 9, 2008: Added partial collection counter by link:../authors.html#mgunderloy[Mike Gunderloy] * November 1, 2008: Added +:js+ option for +render+ by link:../authors.html#mgunderloy[Mike Gunderloy] |