shahryarjb

shahryarjb

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.

Showing Posts 1 to 10

shahryarjb

shahryarjb OP

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

shahryarjb

shahryarjb OP

I did like this, finally see it works:

<.block :for={{id, title, module} <- tailwind_settings} id={id} title={title}>
  <%= Phoenix.LiveView.HTMLEngine.component(
    Code.eval_string("&#{module}/1") |> elem(0),
    [class: "w-6 h-6 mx-auto stroke-current"],
    {__ENV__.module, __ENV__.function, __ENV__.file, __ENV__.line}
  ) %>
</.block>

If you have another way, please let me know
Thanks

LostKobrakai

LostKobrakai

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

shahryarjb

shahryarjb OP

I tried this, I have full name of module and its function which is like this: "Heroicons.inbox_stack", but it needs to have module and function name separately.

LostKobrakai

LostKobrakai

I’d suggest changing the input then to e.g. provide the function name only. Or the module and function name separately.

shahryarjb

shahryarjb OP

Yes, you’re right, I just have one question,

<.block :for={{id, title, module} <- tailwind_settings} id={id} title={title}>
  <%= Phoenix.LiveView.HTMLEngine.component(
    Code.eval_string("&#{module}/1") |> elem(0),
    [class: "w-6 h-6 mx-auto stroke-current"],
    {__ENV__.module, __ENV__.function, __ENV__.file, __ENV__.line}
  ) %>
</.block>

This code is working now, I have no problem. The module is hard-coded, and I do not get it from user input, with these situations I am going to face with security issue?

mustela

mustela

Hey all! It seems that HTMLEngine.component is no longer there… does anyone have another way of dynamically render a component?

mustela

mustela

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews