bullet-train-co / nice_partials

A little bit of magic to make partials perfect for components.
MIT License
298 stars 16 forks source link

Slots #97

Open collimarco opened 1 year ago

collimarco commented 1 year ago

It would be nice to have a feature like this:

https://viewcomponent.org/guide/slots.html

For example, if you want to create a nav component, you may want to add multiple links:

<%= render "components/nav" do |partial| %>
  <% partial.link "Link 1", page_path %>
  <% partial.link "Link 2", another_path %>
<% end %>
kaspth commented 1 year ago

Yeah, good point, we don't have anything quite like this out of the box — iirc we'll try to to_s these values immediately.

I've been trying to think of a way to collect these Array like values and defer the to_s, but it's not the easiest and could have other implications.

We do have the ability to yield into a block. I haven't tried this, but I'm wondering if this would be a fit?

<%= render "components/nav" do |partial| %>
  <% partial.link { _1.link_to "Link 1", page_path } %>
  <% partial.link { _1.link_to "Link 2", another_path } %>
<% end %>

<%# app/views/components/_nav.html.erb %>
<%# Here we're yielding an element that the rendering block above can finalize by passing a block. %>
<% partial.link.yield with_options(class: "text-m4", data: { controller: "title" }) %>
<a href="…" class="text-m4" data-controller="title">Link 1</a>
<a href="…" class="text-m4" data-controller="title">Link 2</a>
kaspth commented 1 year ago

cc @seanpdoyle I'm wondering if you've run into something like this and how you've handled it in your use of Nice Partials.

julianrubisch commented 7 months ago

I ran into this, we should probably add it to the README. I'll set something up.

kaspth commented 7 months ago

@julianrubisch sounds good!

collimarco commented 5 days ago

Any updates on this?