Recently I attended a conference and two separate topics related to Templates sparked a debate off-stage about Logic vs No-logic View Templates. Folks were very passionate about the side they represented. It was great to have those discussions since I learned lot more from those debates than the sessions that were presented. I really hope there are more debates than sessions in future conferences since we actually learn more by hearing multiple viewpoints.
While everyone involved in the discussion had different opinions on what the ideal templating solution should look like, we all agreed that templates should…
- not include business logic
- not include lot of logic
- be easy to read
- be easy to maintain
The rules, best practices, performance etc aside, this is what I prefer. My preferences came out of my experience dealing with templates for medium to complex applications.
What I am for is “less-logic” templates in place of “logic-less” templates (thanks Veena Basavaraj(@vybs) for the term “less-logic templates”!).
A good templating language should offer, at a minimum, the following things:
- Clean and easy-to-read syntax (including freedom to use whitespace that will not show up in output)
- Structural logic like Conditionals, Iterations/Loops, Recursions etc.,
- Text/Token replacement
A great templating language should ALSO offer the following features:
- Ability to be rendered on the server and the client
- Easy to learn with as few new concepts as possible
- Simple template inheritance
- Easily debuggable
- Great error reporting (line numbers, friendly messages, etc.)
- Extensible and customizable
- Localization support
- Resource URL management for images, scripts and CSS
I feel that a logic-less template is too rigid and hard to work with due to the imposed restrictions. Here is why I am not a big fan of logic-less templates:
- If I am writing a logic-less template then I need to make my view model bloated with a ton of getters for the raw data. Therefore, a logic-less template is usually accompanied by a messy and difficult to maintain view model.
- Every time I need to add a new variation to my view I need to then update both the view model and the template (even for simple variations)
- Too much “massaging” of the data is required to get it ready for the template
- A lot of helper methods must be added to offset the rules of a logic-less template
Regarding the argument of better performance with logic-less templates– while logic-less templates might have a simpler implementation, you will still have to pay a price for the additional preprocessing/massaging of the data that is required before the data gets to template renderer. While a templating language that allows more logic might have a more complex implementation, if the compiler is done correctly then it can still produce very efficient compiled code. For those reasons, I would argue that a solution involving templates with more logic will often perform a lot better than a similar solution based on logic-less templates.
There is no doubt that there are advantages like simplicity, readability and possibly some performance gains associated with logic-less templates like Mustache, but I do not think the tradeoffs are fully justified in practice.
Logic in templates isn’t really a bad thing as long as it doesn’t come in the way of readability and maintainability of the templates.
Love to hear your thoughts!
Credits: Many thanks to my colleague Patrick Steele-Idem(@psteeleidem) for helping me write this blog. He is working on some cool stuff like RaptorJS and a new templating language; be sure to check them out when they are open-sourced in the very near future.