I am digging deeper and deeper into Elixir and recently I have been using Phoenix to play around with some web apps. Soon I was to deal with EEx, which stands for Embedded Elixir and is a templating engine for Elixir. It works like Jinja for Python or ERB for Ruby.
EEx has a wonderful feature that is also used in Phoenix where the template is available as a function in the View module. It then gets called with the assigns as a parameter and returns just a binary with the content.
I wondered how this was implemented internally and of course you can invoke it also outside of the framework (and use it as a template for e.g. configruation files). First of all create the template in an
And create a Render module that will render the template and a function
You can then use the defined function named
greet and call it:
The code behind this makes heavy use of meta programming features in Elixir. There is a
defmacro that loads and compiles the template and then defines a function for the given name that uses the compiled template.
Of course this is not only useful for the web but also for configuration files or other files that can be generated by Elixir code.