DEV Community

hong
hong

Posted on

Truth About Template Engines

Intro

I'm writing about content template engines today like Shopify's liquid,
Python's Jinja, Mustache and the others. I am here to finally answer these
burning questions I'm sure you all have.

  • What's better Logic-less vs Logic-full?
  • Why are there so many?
  • How to choose one?

What qualifies me to pass judgement on templating engines? I have written web and email templates for some of the biggest fortune 500 companies. Some of which have been large gnarly order confirmations and airline reservations full of glorious, unreadable business logic.

This is part 1 of a 2 part series.

Let's define logic

Logic-less templates became popular with the arrival of Mustache. Except
Mustache is not really logic-less. There's control flow, looping and even
custom code via lambdas. If you use Mustache without lambdas, I would consider this as close to logic-less as you can get while still being useful.

Full logic in my opinion is when you are freely able to mix both content and
any arbitrary code into your templates. Examples of such are ERB, XSLT and
Underscore.

Some where in-between are semi-logic templates. Logic can be introduced via pre-defined filters and/or custom filters. Semi-logic templates, if abused, can lead to full logic templates. This category includes Liquid, Jinja2, Handlebars, Nunjucks and many others.

'View as an app' frameworks like React and Vue, I would consider to be full logic templating engines plus everything including the kitchen sink.

Logic-less vs Full Logic

In a perfect world your data lines up exactly how you want it to look in your presentation layer. You never need to write any business logic in your templates. This is where logic-less templates is a perfect match and what everyone should strive for.

These style templates are very easy to read. Anyone can understand and write these. Contrast this with full logic that are hard to read and hard to write.

So why use full logic? There are advantages to full logic templates. They are extremely flexible and you can hack quick fixes when you don't want to write complicated controller logic. I've written some crazy business logic into templates because there are always those clients whose requirements are never sane.

In my experience with working with many clients, the data almost never lines up with the presentation requirements.

Know your audience

Not everyone is a programmer. From my experience, anyone trying to teach anything beyond simple control structures to a large group of non-programmers, is going to have a bad time. Having your template engine be simpler gives it access to a broader range of technical expertise.

Imagine if Shopify had used ERB. This would have been a headache to train all customers and have random scripts to solve for business logic. Template writers are not backend coders. This would have also have been a big risk given anyone access to freely execute ruby code through templates.

Even for large teams of experienced programmers, I would still say it's an anti-pattern to use a full logic templating engine. Full logic just leads to HTML content mixed with ugly code. The logic embedded in the code will be hard to test and will introduce bugs.

I wrote XSLT in a large team of experienced technologists. XSLT is in itself a verbose and complex language for templates. Simple changes were fine, but anything greater required hours to write and test. Yet we still did it, largely to solve for gaps in our product.

Is it ever okay to go full logic?

Yes, I would say under certain circumstances, it's okay to use full logic templates. If your team is small and experienced developers you will be fine. However if you find yourself leaning on it too much, it could mean there are some problems with your design. You will start building technical debt when your team becomes larger and less experienced.

Why are there so many?

They are fairly straightforward to write. It seems like each language has their own versions of full logic and semi. Python and JavaScript seem to be leading the pack with more still being created.

Another reason is the 'Not Invented Here Syndrome'. Shopify's Liquid and Mozilla's Nunjucks might be guilty of this. Although it is probably more for legal reasons.

Template engines evolve. For example Twitter liked Mustache, but added template inheritance and wrote Hogan. Ember's Handlebars also built off of Mustache by adding an AST to make sure generated templates are html correct. This is nothing new in the open source world.

Overall I think the number of template engines is good. A template engine can be very personal. Repeating what I mentioned above, what one person needs in a template engine another may not. Perhaps programmer's just want to scratch an itch.

How to choose the one?

You always want to be further away from logic-full as you can. If you can get by with just Mustache and not use lambdas; you should do it. Otherwise Logic-less with some filters/helpers like Liquid and Handlebars is probably the sweet spot.

But please never go full logic.

Which one do I like best? None. The current crop of template engines are all adequate, but not perfect. Certainly room for another templating engine.

Top comments (1)

Collapse
 
ayyappa99 profile image
Ayyappa

Only concern with logic-less template engine is the template gets too complicated and difficult to manage as the size goes bigger. At-least there should be a scope for adding a conditional (for example jMustache is pretty easy to learn but unfortunately extra things need to be done for a conditional)