Skip to content

Commit 44bce92

Browse files
committed
Add faster capture method for components
1 parent a2d5d55 commit 44bce92

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/view_component/base.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,35 @@ def render_in(view_context, &block)
9292
@__vc_content_evaluated = false
9393
@__vc_render_in_block = block
9494

95-
self.parent = view_context
96-
97-
top_level_parent = @_top_level_parent = parent&.top_level_parent || parent
98-
top_level_parent.output_buffer ||= ActionView::OutputBuffer.new
99-
@output_buffer = top_level_parent.output_buffer
100-
top_level_parent.children << self
101-
10295
before_render
10396

10497
if render?
105-
capture { render_template_for(@__vc_variant).to_s + _output_postamble }
98+
if view_context.output_buffer
99+
self.top_level_parent = view_context.top_level_parent || view_context
100+
top_level_parent.output_buffer ||= ActionView::OutputBuffer.new
101+
@output_buffer = top_level_parent.output_buffer
102+
top_level_parent.children << self
103+
else
104+
@output_buffer = ActionView::OutputBuffer.new
105+
end
106+
107+
__vc_fast_call_capture { render_template_for(@__vc_variant).to_s + _output_postamble }.tap do
108+
top_level_parent.children.delete(self) if top_level_parent
109+
end
106110
else
107111
""
108112
end
109113
ensure
110114
@current_template = old_current_template
111115
end
112116

117+
def __vc_fast_call_capture
118+
value = nil
119+
with_output_buffer { value = yield }
120+
121+
ERB::Util.html_escape(value.to_s)
122+
end
123+
113124
# EXPERIMENTAL: Optional content to be returned after the rendered template.
114125
#
115126
# @return [String]

0 commit comments

Comments
 (0)