shahryarjb

shahryarjb

Call a LiveView/Phoenix component dynamically

Imagine you have a core component for LiveView/Phoenix component, and it has some static component like icon

For example:

<Heroicons.inbox_stack class="w-6 h-6 mx-auto stroke-current" />

You want to load it in a loop something like this:

<.block :for={{id, title, icon_module} <- tailwind_settings} id={id} title={title}>
  <icon_module class="w-6 h-6 mx-auto stroke-current" />
</.block>

How can we implement it?

Thank you in advance.

First Post!

shahryarjb

shahryarjb

I found Phoenix.LiveView.HTMLEngine.component, but I could not use with string name of Elixir module and function like: "Elixir.Heroicons.inbox_stack"

<.block :for={{id, title, module} <- tailwind_settings} id={id} title={title}>
  <%= Phoenix.LiveView.HTMLEngine.component(
    String.to_existing_atom(module),
    [class: "w-6 h-6 mx-auto stroke-current"],
    {__ENV__.module, __ENV__.function, __ENV__.file, __ENV__.line}
  ) %>
</.block>

This function needs 3 arity and I can’t use apply, so String.to_existing_atom(module) is just used for converting to module

Do you have any idea?
Thanks

Most Liked

LostKobrakai

LostKobrakai

Function.capture/3 is what you’re looking for.

Last Post!

mustela

mustela

You rock @LostKobrakai ! Thanks a lot! I need to find a “public” api to do this before it changes again.

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New