Rendering objects using Handlebars
Handlebars is a template language that adds minimal syntax to HTML. Its goal is to minimize the amount of logic present in the template and force the passed model object to correspond to what should be rendered in the view.
In this recipe, we will demonstrate some of the advantages and shortcomings of Handlebars using a simple example. We're going to render a user greeting based on the time of the day.
Getting ready
We need to download Handlebars from https://github.com/wycats/handlebars.js. The browser version is in the dist
directory. Create a directory for the example and copy handlebars.js
to this directory, or download directly (on Linux):
wget https://raw.github.com/wycats/handlebars.js/master/dist/handlebars.js
How to do it...
Let's write the code:
Create
index.html
containing aname
input, agreeting
placeholder, and the Handlebars template:<!DOCTYPE HTML> <html> <head> <title>Displaying objects with Handlebars</title> <...