RobinBoers
Renders Jinja templates, either from disk or defined at runtime, with support for all native Jinja features (including extends).
It does this by wrapping the real Jinja library, running in an embedded Python interpreter, inside a GenServer with a nice Elixir API. It uses Pythonx for this.
You start it by adding Jinja to your application supervision tree. There are two loaders:
The default loader is :dict. This allows you to register templates at runtime, for the lifetime of your application. Templates can be loaded and rendered as such:
Jinja.load_template("hello", "hewwo {{ name }}") # => :ok
Jinja.render_template("hello", %{name: "Robin"}) # => {:ok, "hewwo Robin"}
The :path loader allows you to specify a directory on disk to load templates from. When configured, the load_template/2 function will be unavailable.
children = [
{Jinja,
loader: :path,
from: Application.app_dir(:your_app, ~w(lib your_app_web templates))
}
]
# Loads template from lib/your_app_web/templates/hello.html
Jinja.render_template("hello.html", %{name: "Robin"}) # => {:ok, "hewwo Robin"}
The library is available on GitHub and Codeberg:
- Codeberg: https://codeberg.org/RobinBoers/jinja
- Documentation: https://hexdocs.pm/jinja/Jinja.html
- Github:
https://github.com/RobinBoers/jinja
Let me know what you think!
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 3 of 3 Posts
krasenyp
First, why? Second, what happens when an overwhelmingly many requests arrive and each and every one ends up calling the Jinja process?
RobinBoers
I wanted to use Jinja templates in Elixir for a building a CMS. Jinja is well-known, supported in other languages as well and makes it a good fit for a headless CMS. Something like HEEx cannot be consumed by other programs/frontends and I am not a huge fan of Liquid.
Pythonx runs a single interpreter under the hood (it does not launch a new interpreter instance for every request made). You can always create multiple instances if that works better for your architecture.
nayibor
did you try erlydtl.
its been around for while and has a lot of features.