Is there any implementation of the Twig templating language for Phoenix?

I don’t know if you are familiar with PHP and particularly Twig. My team is familiar with it and as we may have to work on a new Phoenix project, using a known syntax could be one less thing to learn up-front. Also, there are two main features that we do not want to lose : extends and commented blocks.

It looks like Django templating language. There is an erlang implementation but it does not seem to have Phoenix support. There is a binding for phoenix: phoenix_dtl but it’s old and does not work anymore.

So, before I dig into those two packages, I would like to know if somebody has already done that before.

Thank you

1 Like

It’s a valid and good question but I want to draw your attention to the fact how insanely efficient EEx is – that might be a reason why people in the Elixir/Phoenix ecosystem are put off from using alternative templating engines.

If it’s an educational project, sure, go ahead and revive the projects – or rewrite them. But have in mind that people like myself will definitely compare performance if you make such a templating library. :stuck_out_tongue:

2 Likes

Well I guess its mostly a parser thing : any template syntax should be transformable to a function lie EEx does. And the performance would be identical. No ?

In theory, yes. I’d be curious to see how you do it in practice.

Based on the docs, extends has a lot of uses - but the basic pattern is probably reproducible in Phoenix with careful use of layouts

EEx supports commenting chunks with <%# ...anything including newlines... %>, though the example in the Twig docs of using that feature seems like it would be better solved using source control (turning off unused code).

Twig also supports an extensive API surface for adding new filters / tags / etc, which even if it existed in Elixir wouldn’t look much like the object-oriented approach used in PHP.

The other challenge you’d face using Twig is that Symfony provides a ton of other plumbing that Twig leverages that wouldn’t be immediately available - things like form that are comparable to what’s provided by phoenix_html. It wouldn’t be an impossible task, but it would be a lot of work just so your team doesn’t need to learn EEx.

3 Likes

Well we do not want Twig, what we want is merely extend and the Twig syntax we are used too. EEx commenting does not support commenting other EEx expressions. Filters, tags, etc… would simply be functions defined in the classic phoenix view modules. We want to move away from Symfony.

Unfortunately layouts only support @inner_content. We want to migrate an app that uses blocks: title, styles blocks, main content, side bar blocks, etc… There is a way though.

1 Like

I think surface can be used with or without liveview. It has a syntax in the line of {{...}} and should be more approachable?

Thanks but I just wanted to know if someone has done something with phoenix and the twig templates family (twig/dtl/jinja/swig/…).

Otherwise we will just use EEx.

Correct. In fact, how we made EEx faster is one of the few videos I have in my Twitch (back when I was streaming): Twitch

3 Likes

What have you done! This makes me want to give it a go :smiley:

FWIW on that link - it uses the deprecated style where layouts call render - that produces a warning on current Phoenix and will go away in the next version:

2 Likes