temple icon indicating copy to clipboard operation
temple copied to clipboard

[feature] multiple instances of the same slot name

Open mhanberg opened this issue 4 years ago • 0 comments

it would be helpful to be able to define many slots with the same name and to be able to loop over them from inside the component.

This example also includes the ability to tag a slot definition with some metadata.

c Table, items: @items do
  slot :column, label: "Name", %{item: item} do
    strong do: item.name
  end

  slot :column, label: "SKU", %{item: item} do
    span do: item.sku
  end

  slot :column, label: "Manufacturer", %{item: %{manufacturer: m}} do
    a href: m.website_url, class: "text-blue-500 hover:underline" do
      m.name
    end
  end
end

# definition
defmodule Table do
  import Temple.Component

  render do
    table do
      thead do
        tr do
          for col <- @slots[:columns] do
            th do
              col.meta.label
            end
          end
        end
      end

      tbody do
        for item <- @items do
          tr do
            for col <- @slots[:column] do
              slot col, item: item
            end
          end
        end
      end
    end
  end
end

mhanberg avatar May 23 '21 20:05 mhanberg