Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strategies for rendering block content #32

Closed
jensljungblad opened this issue Mar 25, 2019 · 1 comment
Closed

Strategies for rendering block content #32

jensljungblad opened this issue Mar 25, 2019 · 1 comment

Comments

@jensljungblad
Copy link
Owner

jensljungblad commented Mar 25, 2019

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
@jensljungblad
Copy link
Owner Author

Closed by #42. Went with the first alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant