From bbbc45156bb2f903513116036619b78065f6d7e8 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 2 Aug 2007 20:10:03 +0000 Subject: Added partial layouts (see example in action_view/lib/partials.rb) [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7261 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_view/base.rb | 12 ++++++- actionpack/lib/action_view/partials.rb | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 63ec0021b0..a8e6655bd6 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -304,7 +304,17 @@ module ActionView #:nodoc: elsif options.is_a?(Hash) options = options.reverse_merge(:type => :erb, :locals => {}, :use_full_path => true) - if options[:file] + if options[:layout] + path, partial_name = partial_pieces(options.delete(:layout)) + + if block_given? + @content_for_layout = capture(&block) + concat(render(options.merge(:partial => "#{path}/#{partial_name}")), block.binding) + else + @content_for_layout = render(options) + render(options.merge(:partial => "#{path}/#{partial_name}")) + end + elsif options[:file] render_file(options[:file], options[:use_full_path], options[:locals]) elsif options[:partial] && options[:collection] render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:locals]) diff --git a/actionpack/lib/action_view/partials.rb b/actionpack/lib/action_view/partials.rb index 9da33d2ac8..e26ca499d0 100644 --- a/actionpack/lib/action_view/partials.rb +++ b/actionpack/lib/action_view/partials.rb @@ -44,6 +44,63 @@ module ActionView # <%= render :partial => "advertisement/ad", :locals => { :ad => @advertisement } %> # # This will render the partial "advertisement/_ad.erb" regardless of which controller this is being called from. + # + # == Rendering partials with layouts + # + # Partials can have their own layouts applied to them. These layouts are different than the ones that are specified globally + # for the entire action, but they work in a similar fashion. Imagine a list with two types of users: + # + # + # Here's the administrator: + # <%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %> + # + # Here's the editor: + # <%= render :partial => "user", :layout => "editor", :locals => { :user => editor } %> + # + # + # Name: <%= user.name %> + # + # + #
+ # Budget: $<%= user.budget %> + # <%= yield %> + #
+ # + # + #
+ # Deadline: $<%= user.deadline %> + # <%= yield %> + #
+ # + # ...this will return: + # + # Here's the administrator: + #
+ # Budget: $<%= user.budget %> + # Name: <%= user.name %> + #
+ # + # Here's the editor: + #
+ # Deadline: $<%= user.deadline %> + # Name: <%= user.name %> + #
+ # + # You can also apply a layout to a block within any template: + # + # + # <% render(:layout => "administrator", :locals => { :user => chief }) do %> + # Title: <%= chief.title %> + # <% end %> + # + # ...this will return: + # + #
+ # Budget: $<%= user.budget %> + # Title: <%= chief.name %> + #
+ # + # As you can see, the :locals hash is shared between both the partial and its layout. module Partials private # Deprecated, use render :partial -- cgit v1.2.3