From e4e1388318cd281bf27114b608487330ae1a58df Mon Sep 17 00:00:00 2001
From: Sergio Arbeo <serabe@gmail.com>
Date: Tue, 6 Mar 2012 16:20:56 +0100
Subject: Adds :layout option to render :partial when a collection is given.

---
 actionpack/lib/action_view/renderer/partial_renderer.rb      | 10 +++++++++-
 actionpack/test/fixtures/test/_b_layout_for_partial.html.erb |  1 +
 actionpack/test/template/render_test.rb                      |  4 ++++
 3 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 actionpack/test/fixtures/test/_b_layout_for_partial.html.erb

(limited to 'actionpack')

diff --git a/actionpack/lib/action_view/renderer/partial_renderer.rb b/actionpack/lib/action_view/renderer/partial_renderer.rb
index 3628b935b7..ac4c8db50e 100644
--- a/actionpack/lib/action_view/renderer/partial_renderer.rb
+++ b/actionpack/lib/action_view/renderer/partial_renderer.rb
@@ -238,7 +238,14 @@ module ActionView
         spacer = find_template(@options[:spacer_template]).render(@view, @locals)
       end
 
+      if layout = @options[:layout]
+        layout = find_template(layout)
+      end
+
       result = @template ? collection_with_template : collection_without_template
+      
+      result.map!{|content| layout.render(@view, @locals) { content } } if layout
+      
       result.join(spacer).html_safe
     end
 
@@ -342,9 +349,10 @@ module ActionView
         locals[as] = object
         segments << template.render(@view, locals)
       end
-
+      
       segments
     end
+    
 
     def collection_without_template
       segments, locals, collection_data = [], @locals, @collection_data
diff --git a/actionpack/test/fixtures/test/_b_layout_for_partial.html.erb b/actionpack/test/fixtures/test/_b_layout_for_partial.html.erb
new file mode 100644
index 0000000000..e918ba8f83
--- /dev/null
+++ b/actionpack/test/fixtures/test/_b_layout_for_partial.html.erb
@@ -0,0 +1 @@
+<b><%= yield %></b>
\ No newline at end of file
diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb
index 7347e15373..122b07d348 100644
--- a/actionpack/test/template/render_test.rb
+++ b/actionpack/test/template/render_test.rb
@@ -238,6 +238,10 @@ module RenderTestCases
   def test_render_partial_with_nil_values_in_collection
     assert_equal "Hello: davidHello: Anonymous", @view.render(:partial => "test/customer", :collection => [ Customer.new("david"), nil ])
   end
+  
+  def test_render_partial_with_layout_using_collection_and_template
+    assert_equal "<b>Hello: Amazon</b><b>Hello: Yahoo</b>", @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ])
+  end
 
   def test_render_partial_with_empty_array_should_return_nil
     assert_nil @view.render(:partial => [])
-- 
cgit v1.2.3


From 6e0a763d0047dcaae26fb691757c3407a20e4a84 Mon Sep 17 00:00:00 2001
From: Sergio Arbeo <serabe@gmail.com>
Date: Wed, 7 Mar 2012 00:06:42 +0100
Subject: Adds line to change log and update documentation.

---
 actionpack/CHANGELOG.md                            |  2 ++
 .../lib/action_view/renderer/partial_renderer.rb   | 37 ++++++++++++++++++++++
 2 files changed, 39 insertions(+)

(limited to 'actionpack')

diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index 869d4704dd..78ac05389c 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,5 +1,7 @@
 ## Rails 4.0.0 (unreleased) ##
 
+* Adds support for layouts when rendering a partial with a given collection. *serabe*
+
 * Allows the route helper `root` to take a string argument. For example, `root 'pages#main'`. *bcardarella*
 
 * Forms of persisted records use always PATCH (via the `_method` hack). *fxn*
diff --git a/actionpack/lib/action_view/renderer/partial_renderer.rb b/actionpack/lib/action_view/renderer/partial_renderer.rb
index ac4c8db50e..245a19deec 100644
--- a/actionpack/lib/action_view/renderer/partial_renderer.rb
+++ b/actionpack/lib/action_view/renderer/partial_renderer.rb
@@ -158,6 +158,43 @@ module ActionView
   #     Name: <%= user.name %>
   #   </div>
   #
+  # If a collection is given, the layout will be rendered once for each item in the collection. Just think
+  # these two snippets have the same output:
+  #
+  #   <%# app/views/users/_user.html.erb %>
+  #   Name: <%= user.name %>
+  #
+  #   <%# app/views/users/index.html.erb %>
+  #   <%# This does not use layouts %>
+  #   <ul>
+  #     <% users.each do |user| -%>
+  #       <li>
+  #         <%= render :partial => "user", :locals => { :user => user } %>
+  #       </li>
+  #     <% end -%>
+  #   </ul>
+  #
+  #   <%# app/views/users/_li_layout.html.erb %>
+  #   <li>
+  #     <%= yield %>
+  #   </li>
+  #
+  #   <%# app/views/users/index.html.erb %>
+  #   <ul>
+  #     <%= render :partial => "user", :layout => "li_layout", :collection => users %>
+  #   </ul>
+  #
+  #   Given two users whose names are Alice and Bob, these snippets return:
+  #
+  #   <ul>
+  #     <li>
+  #       Name: Alice
+  #     </li>
+  #     <li>
+  #       Name: Bob
+  #     </li>
+  #   </ul>
+  #
   # You can also apply a layout to a block within any template:
   #
   #   <%# app/views/users/_chief.html.erb &>
-- 
cgit v1.2.3