How can one render a Liveview .heex file dynamically?

I have a small side project I am working on for someone else and one feature they are looking for is the ability to create liveview pages dynamically on a staging/production running app via an admin panel (the user will enter a name and the slug and module name will be created dynamically based on that)

What I’m trying to figure out is if I were to create the most basic liveview file (mount and render function), is there a way to also compile its corresponding beam file in runtime?

I saw that there is a way to pass in a .beam file to the load path, so calling it seems feasible, it’s just the actual generating part I am confused about.

2 Likes

I would strongly suggest using some other mechanism for dynamic templates (like mustache) other than heex. Dynamic code loading, even done by admins, is a great way to cause all kinds of trouble because, at the end of the day, you’re enabling arbitrary code execution within your application.

FWIW just that you’d be using something like mustache doesn’t mean it can’t be live as long as it’s rendered inside of a live view that knows how to trigger a re-render of the mustache content.

5 Likes

+1 on using a safe templating language, you could evaluate the solid library which uses liquid, the same language as jekkyl

5 Likes

Thanks @benwilson512! Yea, I had a feeling that was the case, but didn’t want to limit the context of question with that presumption.

In relation to the content still being live, and pardon if I interpret this wrong, are you saying I could use a custom liveview module that renders whatever mustache content based on some parameter (the current route, for example)? Essentially, if I give the user the ability to create a page, under the hood I could just store that slug value and pass that to the existing liveview module and render the content for that route?

(Big fan of the graphql work btw, that piece was the first tooling I used when I got started and got me hooked on elixir/phoenix!)

I heard of liquid, but wasn’t aware of solid! This looks like the perfect tool for my use case, thanks a bunch @mayel!

1 Like