If you are just getting starting with Node.js from this blog post, then you are in the right place. When I started NodeJs, I was very confused about templating in Node.js and choosing the best templating engine, as there is a huge list of options.
Let's start with some names of templating engines. First, though, please do some research on the basics of why we need them — if I were to cover them here, this post would turn into a book! Here's a list to help you get started:
After two days of research, I found an awesome web app that allows you to choose your requirements and find the best suitable engine. Check it out here.
Next, let's dive a little deeper into some of these I'll explain which one is my favorite later in this post.
doT is not so easy to use as a template engine, but it does fulfill all of these production questions:
- Is your app is being used both for clients and servers?
- Does your template have huge logic?
- Do you need to make your app faster?
- Do you need to pre-compile templates?
And much more...
You are probably familiar with Jade. It's like magic; less writing and complete relaxation. But when I try to use it with Node.js, when I pass the template to the developer, it has to be converted into HTML, then again to Jade — so it is a bit time-consuming.
Swig does not abstract HTML syntax from you (like, for example, how Jade does), giving you a certain feeling of control over the markup. You can use AngularJS syntax here with Swig and yes, it supports block functionality, as well.
Which One Is the Winner?
I personally liked using EJS the best — maybe I'm biased because I started learning Node.js with it. But it depends on your understanding and requirements for the project. The first priority for me is performance, and the second is ease of writing code. Although doT does an awesome job with performance, its syntax will not make you comfortable if you are not a pure JS-loving dev.