Currently the content of the block of a component or element is rendered using to_s, like this:
<div class="alert">
<%= alert %>
</div>
<%= component "alert" do %>
Hello
<% end %>
Upsides:
Short syntax
No need to name a variable
Will never clash with anything user defined
Downsides:
Would need a method for verifying block was given, for validation
?
Alternative 1: Introduce named variable for the captured block
Something like block, or content.
Upsides:
Works the same as with other attributes
Downsides:
More verbose
We need to name the variable
Will potentially clash with user defined attributes and methods
Alternative 2: Leverage content_for
Rails has a content_for (and a content_for?) helper that perhaps could be leveraged:
<div class="alert">
<%= content_for(alert) %>
</div>
<%= component "alert" do %>
Hello
<% end %>
class Element
def initialize
…
@view.content_for(self, &block)
end
end
Upsides:
Familiar syntax
No need to name a variable
Will never clash with anything user defined
Downsides:
Not tested, not sure if it works, or what problems it would bring
Alternative 3: Connect the block to an attribute
This is something I tried out but later discarded because it felt like I was over-engineering things, and muddied the concepts. Basically the user could choose an attribute name for the block to be captured to, and then you could either populate it like an attribute or by passing a block. Something like:
<div class="alert">
<%= alert.message %>
</div>
<%= component "alert" do %>
Hello
<% end %>
class AlertComponent < Components::Component
attribute :message
capture_block_to :message
end
Upsides:
No need to name a variable
Will never clash with anything user defined
Works the same in templates as with other attributes
Downsides
Perhaps blurs concepts together, and perhaps makes the API more difficult to understand at glance
Currently the content of the block of a component or element is rendered using
to_s
, like this:Upsides:
Downsides:
Alternative 1: Introduce named variable for the captured block
Something like
block
, orcontent
.Upsides:
Downsides:
Alternative 2: Leverage
content_for
Rails has a
content_for
(and acontent_for?
) helper that perhaps could be leveraged:Upsides:
Downsides:
Alternative 3: Connect the block to an attribute
This is something I tried out but later discarded because it felt like I was over-engineering things, and muddied the concepts. Basically the user could choose an attribute name for the block to be captured to, and then you could either populate it like an attribute or by passing a block. Something like:
Upsides:
Downsides