Was implementing an analogue of render_many
for a live_component
.
Should have worked like this:
<%= exhibit(@collection, SomeComponent, socket: @socket, as: :some) %>
require Phoenix.LiveView.Helpers
alias Phoenix.LiveView.Helpers, as: View
def exhibit(collection, component, params \ [], socket: socket, as: item_name) when is_list(collection) do
Enum.map(collection, fn item ->
View.live_component socket, component, some: item
end)
end
Well, it seems like this is not possible:
A component must always be returned directly as part of a LiveView template.
For example, this is not allowed:
<%= content_tag :div do %> <%= live_component @socket, SomeComponent %> <% end %>
That's because the component is inside
content_tag
. However, this works:<div> <%= live_component @socket, SomeComponent %> </div>
Components are also allowed inside Elixir's special forms, such as
if
,for
,case
, and friends.<%= for item <- items do %> <%= live_component @socket, SomeComponent, id: item %> <% end %>
However, using other module functions such as
Enum
, will not work:<%= Enum.map(items, fn item -> %> <%= live_component @socket, SomeComponent, id: item %> <% end %>
Tried few other things to make it work, but it seems hopeless.
Any clue how to implement render_many
for a live_component
or I have to live with those ugly for
loops in templates?