Skip to content
Browse files

Allow layout rendering to access current object being rendered when u…

…sing partial + collection
  • Loading branch information...
1 parent 82029c7 commit e72374d0ff8b149bdbf8e14a703cbc2028df520c @carlosantoniodasilva carlosantoniodasilva committed Apr 26, 2012
View
17 actionpack/lib/action_view/renderer/partial_renderer.rb
@@ -283,13 +283,19 @@ def render_collection
end
if layout = @options[:layout]
- layout = find_template(layout)
+ layout = find_template(layout, @locals.keys + [@variable])
end
result = @template ? collection_with_template : collection_without_template
-
- result.map!{|content| layout.render(@view, @locals) { content } } if layout
-
+
+ if layout
+ locals = @locals
+ result.map! do |content|
+ locals[@variable] = @collection[result.index(content)]
+ layout.render(@view, @locals) { content }
+ end
+ end
+
result.join(spacer).html_safe
end
@@ -391,10 +397,9 @@ def collection_with_template
locals[as] = object
segments << template.render(@view, locals)
end
-
+
segments
end
-
def collection_without_template
segments, locals, collection_data = [], @locals, @collection_data
View
1 actionpack/test/fixtures/test/_b_layout_for_partial_with_object.html.erb
@@ -0,0 +1 @@
+<b class="<%= customer.name.downcase %>"><%= yield %></b>
View
8 actionpack/test/template/render_test.rb
@@ -233,11 +233,15 @@ def test_render_partial_with_nil_collection_should_return_nil
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_layout_using_collection_and_template_makes_current_item_available_in_template
+ assert_equal '<b class="amazon">Hello: Amazon</b><b class="yahoo">Hello: Yahoo</b>', @view.render(:partial => "test/customer", :layout => 'test/b_layout_for_partial_with_object', :collection => [ Customer.new("Amazon"), Customer.new("Yahoo") ])
+ end
+
def test_render_partial_with_empty_array_should_return_nil
assert_nil @view.render(:partial => [])
end
@@ -310,7 +314,7 @@ def test_render_inline_with_locals_and_compilable_custom_type
ActionView::Template.register_template_handler :foo, CustomHandler
assert_equal 'source: "Hello, <%= name %>!"', @view.render(:inline => "Hello, <%= name %>!", :locals => { :name => "Josh" }, :type => :foo)
end
-
+
def test_render_knows_about_types_registered_when_extensions_are_checked_earlier_in_initialization
ActionView::Template::Handlers.extensions
ActionView::Template.register_template_handler :foo, CustomHandler

0 comments on commit e72374d

Please sign in to comment.
Something went wrong with that request. Please try again.