Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing of array and block to layout #12875

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions actionview/lib/action_view/renderer/partial_renderer.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -383,20 +383,32 @@ def find_template(path, locals)
@lookup_context.find_template(path, prefixes, true, locals, @details) @lookup_context.find_template(path, prefixes, true, locals, @details)
end end


def layout_path
return nil if @options[:layout].blank?
if String === @options[:layout]
path = @options[:layout]
else
path = partial_path(@options[:layout].first)
end
end

def layout_matches_partial?
@options[:layout] == @options[:partial]
end

def collection_with_template def collection_with_template
view, locals, template = @view, @locals, @template view, locals, template, block = @view, @locals, @template, @block
as, counter = @variable, @variable_counter as, counter = @variable, @variable_counter


if layout = @options[:layout] layout = layout_path unless layout_matches_partial?
layout = find_template(layout, @template_keys) layout = find_template(layout, @template_keys) if layout
end


index = -1 index = -1
@collection.map do |object| @collection.map do |object|
locals[as] = object locals[as] = object
locals[counter] = (index += 1) locals[counter] = (index += 1)


content = template.render(view, locals) content = template.render(view, locals, nil, &block)
content = layout.render(view, locals) { content } if layout content = layout.render(view, locals) { content } if layout
content content
end end
Expand Down
2 changes: 2 additions & 0 deletions actionview/test/fixtures/users/_user.html.erb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= greeting %>: <%= user.name %>
<%= yield user %>
4 changes: 4 additions & 0 deletions actionview/test/lib/controller/fake_models.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def persisted?
end end
end end


class User < Struct.new(:name, :title)
include ActiveModel::Conversion
end

class GoodCustomer < Customer class GoodCustomer < Customer
end end


Expand Down
6 changes: 6 additions & 0 deletions actionview/test/template/render_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ def test_render_layout_with_block_and_other_partial_inside
assert_equal "Before\npartial html\nYield!\nAfter\n", render assert_equal "Before\npartial html\nYield!\nAfter\n", render
end end


def test_render_layout_with_array_and_block
users = [User.new("Mac", "developer"), User.new("Ryman", "pitcher")]
render = @controller_view.render(:layout => users, :locals => { :greeting => "Hello" }) { |user| "#{user.title}" }
assert_equal "Hello: Mac\ndeveloperHello: Ryman\npitcher", render
end

def test_render_inline def test_render_inline
assert_equal "Hello, World!", @view.render(:inline => "Hello, World!") assert_equal "Hello, World!", @view.render(:inline => "Hello, World!")
end end
Expand Down