Phoenix LiveView slot attributes
Today I learned how to define an attribute on a Phoenix LiveView slot using a do block:
slot :column
attr :field, :atom
attr :sortable, :boolean, default: false
end
def table(assigns) do
~H"""
<table>
<tr>
<th :for={col <- @column}>
<%= col.label %>
<.sort_by :if={col.sortable} field={col.field}/>
</th>
</tr>
</table>
"""
end
This way we can use our slots with attributes:
def render(assigns) do
~H"""
<.table>
<:colum field={:name} sortable>Name</:colum>
<:colum field={:price}>Price</:colum>
<:colum>Actions</:colum>
...
</.table>
"""
end
Check this out
Tweet