Handlebars basics: Variables, expressions, and comments

Learn common Handlebars syntax, including variables, property paths, HTML escaping, expressions, and comments.

🎯 Introduction

In this article, you will learn the Handlebars syntax you will use most often: rendering variables, accessing object properties, rendering raw HTML, and writing comments.


🧱 Rendering variables

Wrap a variable name in double curly braces {{ }} to render its value in HTML.

index.js
const Handlebars = require('handlebars');

const template = '<p>Username: {{username}}</p>';
const compiled = Handlebars.compile(template);
const html = compiled({ username: 'Ming' });

console.log(html);

Output:

<p>Username: Ming</p>
Variable names must match the properties of the data object you pass in. Names are case-sensitive.

✨ Accessing object properties

Use dot notation . to access properties in nested objects.

index.js
const template = '<p>City: {{user.address.city}}</p>';
const compiled = Handlebars.compile(template);
const html = compiled({
    user: {
        address: {
            city: 'Shanghai'
        }
    }
});

console.log(html);

Output:

<p>City: Shanghai</p>

Slash notation such as {{user/name}} also works, but dot notation is recommended because it follows familiar JavaScript conventions.


⚡ Raw HTML and escaping

Handlebars escapes variable values as HTML by default to help prevent XSS attacks.

index.js
const template = '<div>{{content}}</div>';
const compiled = Handlebars.compile(template);
const html = compiled({ content: '<script>alert("xss")</script>' });

console.log(html);

Output:

<div>&lt;script&gt;alert("xss")&lt;/script&gt;</div>

If you know the content is safe and want to render raw HTML, use triple curly braces {{{ }}}:

index.js
const template = '<div>{{{content}}}</div>';
const compiled = Handlebars.compile(template);
const html = compiled({ content: '<strong>Important information</strong>' });

console.log(html);

Output:

<div><strong>Important information</strong></div>
Use {{{ }}} with care. Only use it for fully trusted content, such as HTML you construct yourself. Otherwise, it can introduce XSS vulnerabilities.

💡 Comments

Handlebars supports two kinds of comments:

Comments included in the final HTML

<!-- Regular HTML comment -->

Comments excluded from the final HTML

{{! This Handlebars comment does not appear on the page }}

Example:

index.js
const template = '{{! Start of user information }}<p>{{name}}</p>{{! End of user information }}';
const compiled = Handlebars.compile(template);
const html = compiled({ name: 'Ming' });

console.log(html);

Output:

<p>Ming</p>

The template comments are removed and do not appear in the final HTML.


🛠 Putting the examples together

index.js
const Handlebars = require('handlebars');

const template = `
<div>
    <h1>{{user.name}}</h1>
    <p>Bio: {{{user.bio}}}</p>
    {{! This comment is only visible in the template }}
    <p>From: {{user.address.city}}</p>
</div>
`;

const compiled = Handlebars.compile(template);
const html = compiled({
    user: {
        name: 'Hong',
        bio: '<em>Passionate about frontend development</em>',
        address: {
            city: 'Hangzhou'
        }
    }
});

console.log(html);

❓ Questions and answers

Q1: What is the difference between {{ }} and {{{ }}}?

{{ }} escapes HTML, while {{{ }}} outputs content as-is. Use {{ }} by default and reserve {{{ }}} for safe content.

Q2: Does Handlebars throw an error if a variable is missing?

By default, it outputs an empty string instead. For example, {{user.age}} produces no text if user is missing.

Q3: How do Handlebars comments differ from HTML comments?

HTML comments remain in the output. Handlebars comments, written as {{! }}, are removed during rendering.

Q4: Can I use brackets to access properties?

Handlebars supports syntax such as {{user.[name]}}, although dot notation is more common for everyday use.


🧪 Exercise

Write a Handlebars template and render the following data:

index.js
const data = {
    article: {
        title: 'Getting started with Handlebars',
        summary: '<strong>A very practical</strong> template engine'
    }
};

// Write the template and rendering code here
// Render title with an escaped expression and summary as raw HTML

🎉 You have learned the basics of Handlebars syntax! Next, we will use built-in helpers to add conditions and loops to templates.