aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/base.rb12
-rw-r--r--actionpack/lib/action_view/partials.rb57
2 files changed, 68 insertions, 1 deletions
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:
+ #
+ # <!-- app/views/users/index.html.erb -->
+ # Here's the administrator:
+ # <%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %>
+ #
+ # Here's the editor:
+ # <%= render :partial => "user", :layout => "editor", :locals => { :user => editor } %>
+ #
+ # <!-- app/views/users/_user.html.erb -->
+ # Name: <%= user.name %>
+ #
+ # <!-- app/views/users/_administrator.html.erb -->
+ # <div id="administrator">
+ # Budget: $<%= user.budget %>
+ # <%= yield %>
+ # </div>
+ #
+ # <!-- app/views/users/_editor.html.erb -->
+ # <div id="editor">
+ # Deadline: $<%= user.deadline %>
+ # <%= yield %>
+ # </div>
+ #
+ # ...this will return:
+ #
+ # Here's the administrator:
+ # <div id="administrator">
+ # Budget: $<%= user.budget %>
+ # Name: <%= user.name %>
+ # </div>
+ #
+ # Here's the editor:
+ # <div id="editor">
+ # Deadline: $<%= user.deadline %>
+ # Name: <%= user.name %>
+ # </div>
+ #
+ # You can also apply a layout to a block within any template:
+ #
+ # <!-- app/views/users/_chief.html.erb -->
+ # <% render(:layout => "administrator", :locals => { :user => chief }) do %>
+ # Title: <%= chief.title %>
+ # <% end %>
+ #
+ # ...this will return:
+ #
+ # <div id="administrator">
+ # Budget: $<%= user.budget %>
+ # Title: <%= chief.name %>
+ # </div>
+ #
+ # As you can see, the :locals hash is shared between both the partial and its layout.
module Partials
private
# Deprecated, use render :partial