diff --git a/app/models/turbo/streams/tag_builder.rb b/app/models/turbo/streams/tag_builder.rb index 8b6eed84..f51189b9 100644 --- a/app/models/turbo/streams/tag_builder.rb +++ b/app/models/turbo/streams/tag_builder.rb @@ -267,6 +267,8 @@ def render_template(target, content = nil, allow_inferred_rendering: true, **ren content.render_in(@view_context, &block) when content allow_inferred_rendering ? (render_record(content) || content) : content + when block_given? && (rendering.key?(:partial) || rendering.key?(:layout)) + @view_context.render(formats: [ :html ], layout: rendering[:partial], **rendering, &block) when block_given? @view_context.capture(&block) when rendering.any? diff --git a/test/dummy/app/views/application/_partial_with_block.html.erb b/test/dummy/app/views/application/_partial_with_block.html.erb new file mode 100644 index 00000000..6bea2a2c --- /dev/null +++ b/test/dummy/app/views/application/_partial_with_block.html.erb @@ -0,0 +1 @@ +

<%= yield %>

diff --git a/test/streams/streams_helper_test.rb b/test/streams/streams_helper_test.rb index 45d66429..1385e413 100644 --- a/test/streams/streams_helper_test.rb +++ b/test/streams/streams_helper_test.rb @@ -19,6 +19,48 @@ def to_key attr_accessor :formats + test "turbo_stream builder captures block when called without :partial keyword" do + rendered = turbo_stream.update "target_id" do + tag.span "Hello, world" + end + + assert_dom_equal <<~HTML.strip, rendered + + + + HTML + end + + test "turbo_stream builder forwards block to partial when called with :partial keyword" do + rendered = turbo_stream.update "target_id", partial: "application/partial_with_block" do + "Hello, from application/partial_with_block partial" + end + + assert_dom_equal <<~HTML.strip, rendered + + + + HTML + end + + test "turbo_stream builder forwards block to partial when called with :layout keyword" do + rendered = turbo_stream.update "target_id", layout: "application/partial_with_block" do + "Hello, from application/partial_with_block partial" + end + + assert_dom_equal <<~HTML.strip, rendered + + + + HTML + end + test "supports valid :renderable option object with nil content" do component = Component.new(id: 1, content: "Hello, world")