Handlebars is a template engine not a specific tool, so there are multiple ways you can use and install it. Handlebars templates can either be pre-compiled to improve performance, or you can compile them on the client side and have more flexibility with manipulating the templates themselves.
In this chapter we will take a look at downloading and setting up the necessary components to implement both of these methods.
Handlebars is easy to install as a js library since it has no other dependencies. To download the library, just go to http://handlebarsjs.com/ and click on the big download button, this should take you to the script, which you can save on your computer as handlebars.js
:
Installing this file is as simple as adding a script tag to your DOM with the path to this file:
<script src="handlebars.js"></script>
Compiling templates can be an expensive process. If you have a complex template, or even nested templates, it can slow down your production code. The solution to this is compiling the templates ahead of time. Then you will be left with pure JavaScript, which will make your overall page load much faster.
The first thing you will need to pre-compile your templates is node.js, which you can download and set up from http://nodejs.org/download/:
The setup wizard will install both node.js and the node package manager (NPM). With that ready, we can install the Handlebars CLI by running the following command in your terminal:
npm install -g handlebars
You don't have to worry too much about this if you are new to node.js. Basically this will download and place the Handlebars.js app into your path, so that you can use the handlebars
command from your terminal.