Skip to content

Commit 7abcd2c

Browse files
committed
Capture partial block in the components context
1 parent 839fd5c commit 7abcd2c

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

lib/view_component/base.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,17 @@ def render(options = {}, args = {}, &block)
260260
@view_context.render(options, args, &block)
261261
elsif block
262262
__vc_original_view_context.render(options, args) do
263+
# capture the block output in the view context of the component
264+
output = capture(&block)
265+
263266
# Partials are rendered to their own buffer and do not append to the
264267
# original @output_buffer we retain a reference to in #render_in. This
265268
# is a problem since the block passed to us here in the #render method
266269
# is evaluated within the context of ViewComponent::Base, and thus
267270
# appends to the original @output_buffer. To avoid this, we evaluate the
268271
# block in the view context instead, which will append to the output buffer
269272
# created for the partial.
270-
__vc_original_view_context.instance_exec(&block)
273+
__vc_original_view_context.capture { output }
271274
end
272275
else
273276
__vc_original_view_context.render(options, args)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<%= render "shared/yielding_partial" do %>
2+
<%= world %>
3+
<% end %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class PartialWithYieldAndMethodCallComponent < ViewComponent::Base
2+
def world
3+
"world"
4+
end
5+
end

test/sandbox/test/rendering_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,4 +1322,9 @@ def test_render_partial_with_yield
13221322
render_inline(PartialWithYieldComponent.new)
13231323
assert_text "hello world", exact: true, normalize_ws: true
13241324
end
1325+
1326+
def test_render_partial_with_yield_and_method_call
1327+
render_inline(PartialWithYieldAndMethodCallComponent.new)
1328+
assert_text "hello world", exact: true, normalize_ws: true
1329+
end
13251330
end

0 commit comments

Comments
 (0)