Why I chose Swig over Jade templates for my Node+Express project
This post is decidedly nerdier than usual. Bear with me.
I've been working on some home-brewed software using Node and the Express framework. I find building little tools that I'll actually use in my day-to-day business helps me learn new languages and concepts, even if I find myself reinventing a few wheels.
Because Node and Express are so nascent there is a tendency for things to change, not always work as expected and be poorly documented. I ran into some of these issues when looking for a good template system to use (( For a comprehensive list of Node templates with feature comparisons see Paul Armstrong's site)).
Jade seems to be the template system du jour within the Node/Express community. It shares a lot in common Haml which, while I find interesting, is just too much of an abstraction for my tastes. I've stared at enough HTML over the years that my eyes don't have much difficulty parsing what's going on. When I look at Haml/Jade syntax I have to stop and think about what's going on for a moment.
I'm actually partial to Liquid for templates. I'm familiar with it through developing Shopify themes and it's originally written in Ruby, so I've adapted it and extended it for some personal Sinatra projects. I've also found Liquid adaptations for PHP and other languages, so I decided to see if someone had already adapted it to Node. Lo and behold — LiquidNode! Seemed like just the ticket.
Because I was already familiar with Liquid I glossed over the documentation. As it turns out it doesn't currently support the two features I find most useful: partials via include and the date filter. This was enough of a show-stopper that I decided to look into Jade. For reasons that might be particular to my setup I found the way partials are included to be kind of strange, which I could get past, but there wasn't a date filter. The best solution to that seemed to involved loading the moment.js package, exposing it to the application locals and... I'm officially off the Jade bandwagon, until someone can try to convince me again. So I decided to go back to the drawing board and explore some of template systems I was less familiar with.
I settled on a combination of consolidate.js — a brilliant tool for consolidating a ton of different template engines — and Swig. The syntax is practically identical to Liquid so I hardly needed to adjust any of my existing templates (( I had to remove a few
unless blocks, which were a fun way to think a little differently about normal
if blocks, but that was about it.)). Most importantly to me it handled partials exactly the way I expected and included an excellent date filter.
Long story short: if you're starting a new Node + Express project and are in the market for a good template system, give Swig a try!