Wrap a slot in a block #2062
-
class ListComponent < ViewComponent::Base
renders_one :header
renders_many :rows
erb_template <<~ERB
<div class="list-component">
<h2><%= header %></h2>
<% rows.each do |row| %>
<div class="divider"></div>
<%= row %>
<% end %>
</div>
ERB
end Take the above component. Does anyone have any suggestions on how to implement if a caller would like to wrap the <%= render ListComponent.new do |component| %>
<% component.with_header do %>
Hi Everybody!
<% end %>
<% form_with url: "/" do |form| %>
<% [1, 2, 3].each do |i| %>
<% component.with_row do %>
<%= render partial: "partial", local_variables: {item: i, form: form} %>
<% end %>
<% end %>
<% end %>
<% end %>
Almost like this: https://github.com/palkan/view_component-contrib?tab=readme-ov-file#wrapped-components This is close: #1126 (comment) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@jasonbouffard forgive me if I'm misunderstanding your question, but I believe that you need to treat the <% form_with url: "/" do |form| %>
<%= render ListComponent.new(form: form) do |component| %>
<% component.with_header do %>
Hi Everybody!
<% end %>
<% [1, 2, 3].each do |i| %>
<% component.with_row do %>
<%= render partial: "partial", local_variables: {item: i, form: form} %>
<% end %>
<% end %>
<% end %>
<% end %>
|
Beta Was this translation helpful? Give feedback.
@jasonbouffard forgive me if I'm misunderstanding your question, but I believe that you need to treat the
with_row
call as simply a setter. Here is how I would do it: