Skip to content
Merged
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
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ nav_order: 6

## main

* Capture partial block in the components context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Capture partial block in the components context
* Capture partial block in the component's context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, didn't have time. Thanks for the work!

*23tux*

* Resolve deprecation warning for `ActiveSupport::Configurable`.

*Simon Fish*
Expand Down
5 changes: 4 additions & 1 deletion lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,17 @@ def render(options = {}, args = {}, &block)
@view_context.render(options, args, &block)
elsif block
__vc_original_view_context.render(options, args) do
# capture the block output in the view context of the component
output = capture(&block)

# Partials are rendered to their own buffer and do not append to the
# original @output_buffer we retain a reference to in #render_in. This
# is a problem since the block passed to us here in the #render method
# is evaluated within the context of ViewComponent::Base, and thus
# appends to the original @output_buffer. To avoid this, we evaluate the
# block in the view context instead, which will append to the output buffer
# created for the partial.
__vc_original_view_context.instance_exec(&block)
__vc_original_view_context.capture { output }
end
else
__vc_original_view_context.render(options, args)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= render "shared/yielding_partial" do %>
<%= world %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class PartialWithYieldAndMethodCallComponent < ViewComponent::Base
def world
"world"
end
end
5 changes: 5 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1322,4 +1322,9 @@ def test_render_partial_with_yield
render_inline(PartialWithYieldComponent.new)
assert_text "hello world", exact: true, normalize_ws: true
end

def test_render_partial_with_yield_and_method_call
render_inline(PartialWithYieldAndMethodCallComponent.new)
assert_text "hello world", exact: true, normalize_ws: true
end
end
Loading