Handlebars.js vs EJS
When comparing Handlebars.js vs EJS, the Slant community recommends Handlebars.js for most people. In the question“What are the best JavaScript templating engines?” Handlebars.js is ranked 2nd while EJS is ranked 6th. The most important reason people chose Handlebars.js is:
Handlebars is available as a JavaScript library, a [Django](https://github.com/yavorskiy/django-handlebars) module as well as [Java](https://github.com/jknack/handlebars.java), [Ruby](https://github.com/MSch/handlebars-ruby), [Scala](https://github.com/mwunsch/handlebars.scala), [.Net](https://github.com/rexm/Handlebars.Net) & [PHP](https://github.com/zordius/lightncandy) libraries, which means you can use it for frontend and backend templating in the language of your choice.
Ranked in these QuestionsQuestion Ranking
Pros
Pro Clean syntax
Handlebars's syntax is very readable and easy to understand.
Pro Clear separation of logic and markup
If something is not within {{
and }}
, it's not Handlebars. As a result, Handlebars "weaves" through HTML, instead of trying to become an invalid extension of it.
Pro Copy/Paste code from the internet
Examples for Bootstrap or other CSS frameworks are always in HTML. With Handlebards you can just copy and paste the examples in your code. With something like Pug (Jade) you have to convert the HTML to Pug (Jade) first.
Pro Easy to use for templating things other than HTML
The syntax allows the output to be any text and does not contrain the user to HTML output only. There are examples of handlebars being used to produce SQL, javascript and other programming language code.
Pro Easy to use any template also as partials
Templates may be nested and reusable parts can be factored out.
Pro Compiled rather than interpreted templates
Handlebars.js allows you to pre-compile your templates so that the loading time at the client end could be reduced when your templated page is loaded.
Pro Mustache compatible
You can import Mustache templates and add extra functionality, that's provided by Handlebars, on top of them.
Pro Good global helpers support
Pro Logic-less
By design, logic-less templates force you to separate concerns thus helping you avoid future problems with refactoring. It also allow templates to be used with multiple programming languages without changes.
Pro Good paths support
With Handlebars.js you can create bindings with variables inside any path in your application.
Pro Easy to define extensions
With a few lines of code, a new extension (control or templating function) can be implemented. It will be called by the compiled templates.
Pro Complete JavaScript logic
EJS uses all the JS jargon and logic, so if you're proficient in JS, you can use EJS right away.
Pro Same language before and after rendering
Your html/text remains pretty much the same before and after rendering. EJS filters out and performs its functions on any occurrences of its own <%= %>
tags in your template.
Pro Lightning-fast to learn
EJS introduces fairly small amount of new syntax that one has to learn to become fully proficient. The syntax itself is easy to comprehend for anyone who is even somewhat familiar with JavaScript and CSS.
Pro Consistently scores rather well in benchmarks
According to some benchmark tests, EJS is way faster than Jade or Haml.
Pro Powerful error handling
EJS has a really smart error handling mechanism built right into it. It points out to you, the line numbers on which an error has occurred, so that you don't end up looking through the whole template file wasting your time in searching for bugs.
Cons
Con Hard to use documentation
Although the documentation exists and is fairly comprehensive, it's not always clearly written, and there is no search capacity.
Con Handlebars are still an HTML code
Handlebars use the standard HTML syntax with its own {{tags}} for templating. This doesn't add much to readability or design speed.
Con Using partials is cumbersome
"In order to use a partial, it must be registered" using some JavaScript method attached to some global variable.
Con Not much editor support
Handlebars.js doesn't seem to have many text editors that support things like auto-complete, syntax highlighting or error checking for it.
Con Does not play well with Angular.js
.. or any framework where you wish to compile handlebars.js template to the template understanable by the framework.
Con No support for block by default
EJS has no support for the block
functionality which allows you to reuse pieces of templates across different files. Although it can be added to EJS through a third-party library.
Con Cryptic syntax
Much more difficult to read, especially for designer/HTML people who don't write JavaScript.