Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Templating in Node.js With Simple Signup Example

DZone's Guide to

Templating in Node.js With Simple Signup Example

In this article, we will be comparing different Node.js templating engines as well as going through the basics of Node.js.

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

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: 

  1. Mustache

  2. Handlebars

  3. doT

  4. Dust

  5. EJS

  6. Underscore

  7. Jade

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

doT is not so easy to use as a template engine, but it does fulfill all of these production questions:

  1. Is your app is being used both for clients and servers?
  2. Does your template have huge logic?
  3. Do you need to make your app faster?
  4. Do you need to pre-compile templates?

And much more...

Jade

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.

EJS

EJS is CanJS's default template language, which provides live binding when used with Observes. EJS is very easy to use; you write the HTML you want to be in the template, along with a few magic tags where you want dynamic behavior. But EJS does not support block functionality. Your JavaScript should be good for EJS, though.

Swig

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.

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:
node.js ,mongodb ,tutorial ,web dev ,templates ,template engine

Published at DZone with permission of Nikhil Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}