Nicd

Nicd

Dynamic EEx templates for web app

I’m currently making a blog engine for my own use. It has Distillery releases, and is very configurable with the new Distillery config provider. The user can also reverse proxy it with Nginx and serve their own JS and CSS files, if they want to do so. So it should be usable for someone else than me as well, except… templates.

The HTML (and XML for RSS) templates use the EExHTML library and they are compiled in to the app as view functions. This means that a user must compile their own version if they want to change a template. If it was made with Phoenix or Plug, the same thing would happen, as releases are compiled beforehand.

Now (disregarding all the other differences) with PHP, it was very easy for a user to take an app and customise a single PHP file. I realise Elixir does not work this way, but I’m wondering if there is any way to accomplish something similar. Say you had to implement a feature where a user could put their own EEx files into a directory and reboot the release, and the release would pick up the new files and use them instead of the built in templates. How would you accomplish that? You would have to compile the templates at runtime and reload the corresponding modules somehow.

What I’m after is a blog engine where the user can customise any templates they want without compiling their own release. Just get the premade release from GitLab/where-ever, put your own files in a path, and boot it. Does this give anyone any ideas?

Most Liked

josevalim

josevalim

Creator of Elixir

You can do eval_string/eval_file with EEx too: https://hexdocs.pm/eex/EEx.html - just make sure to pass the EExHTML engine as an option.

josevalim

josevalim

Creator of Elixir

If it is for things like blogs, you can cache the result of the rendered template instead.

You don’t need to version it because Elixir modules are versioned in the VM due to hot code reloading. Just keep in mind that this will leak atoms but it is fine if only “admins” can do that.

Nicd

Nicd

This is true, but I was hoping to avoid that, because the use case might be different from a blog too. And even blogs have things like comments. It also would not work for partials that are called in loops with different inputs for each iteration.

I can recompile and reload the same module without compiling to a different version first? When do the other processes pick it up, when they make their next call into it? If a compilation error happens, I assume the old version of the module will remain?

Yeah I don’t think people will reload the templates that often, but of course it needs to be measured and taken into consideration, thanks for the reminder. :slight_smile:

OvermindDL1

OvermindDL1

Immediately on the next fully qualified call to it (ModuleName.function(...)), and yes. Specifically the system keeps around 2 copies of each module that is hot-loaded until no more active references (actively running code) exist to the older one, if there ‘would be’ an even older one than any code actively running it would be killed (and the supervisor would restart it with the new code), but for that to happen in general usage you are either looping manually and near infinitely or you are hot-code reloading new versions extremely rapidly. :slight_smile:

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement