Accessing the template block when using Phlex::DeferredRender #571
|
Hey! When designing new components for our application, I switched from I have a def template(&)
if !turbo_stream? && !turbo_frame?
plain_content(&)
elsif turbo_frame?
frame_content(&)
elsif turbo_stream?
stream_content(&)
end
endHowever, I now wanted to extend this component to support an optional header element = render Modal.new do |c|
- c.with_header("something")
%p I am in the modal bodyI'm using Is there a way to access the output of the Thanks a lot in advance! |
Replies: 1 comment 12 replies
|
Hey @stex, we decided against passing the block to the template method when using deferred render because it could likely lead to confusing interfaces. Take this for example: render Card.new do |c|
p { "Before" }
c.title { "Hello" }
p { "After" }
endIf the card was implemented in the "builder style" like this, you would get the expected output. class Card < Phlex::HTML
def template(&)
article(&)
end
def title(&)
h1(class: "title", &)
end
endBut if the card was implemented with deferred render, both paragraphs would appear after the title. class Card < Phlex::HTML
include Phlex::DeferredRender
def template
article do
h1(class: "title", &@title)
yield
end
end
def title(&block)
@title = block
end
endWe thought it would be better to encourage people to ignore the output of the block itself and instead use specific methods for each content slot. In this case, we could implement a render Card.new do |c|
c.title { "Hello" }
c.content do
p { "Hello" }
p { "World" }
end
end |
Hey @stex, we decided against passing the block to the template method when using deferred render because it could likely lead to confusing interfaces. Take this for example:
If the card was implemented in the "builder style" like this, you would get the expected output.
But if the card was implemented with deferred render, both paragraphs would appear after the title.