O'Reilly logo

Web Development with MongoDB and NodeJS - Second Edition by Jason Krol, Bruno Joseph D'mello, Mithun Satheesh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The basics of Handlebars

Handlebars is a really simple and easy-to-use templating framework. Let's go through and explore the basic syntax of writing a Handlebars template.

Binding an object to the template

Let's assume the following JavaScript object is passed to a Handlebars template:

var model = {
  name: 'World'
};

The template file itself will contain the following markup:

<div>
  Hello {{ name }}!
</div>

This file will render to a browser in the following way:

Hello World!

Embedding presentation logic

Of course, there's a lot more that you can do than just this! Handlebars also supports conditional statements:

var model = { name: 'World', description: 'This will only appear because its set.' }; <div> Hello {{ name }}!<br/><br/> {{#if description}} <p>{{description}}</p> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required