Getting started with Handlebars: What is a template engine?

Learn what Handlebars does, when to use it, and how to install it and render your first data with a minimal example.

🎯 Introduction

In this article, you will learn what Handlebars is, which problems it solves, and how to get started in a project. We will install Handlebars, write a first template, and render JSON data as HTML.


🧱 What is a template engine?

In real projects, much of a page's content comes from backend APIs rather than being hardcoded. Examples include user profiles, product lists, and news articles.

Building HTML by concatenating strings quickly becomes difficult to maintain:

index.js
const user = { name: 'Ming', age: 18 };
const html = '<div><h1>' + user.name + '</h1><p>Age: ' + user.age + '</p></div>';

A template engine solves this problem by separating HTML structure from data:

  • Define the HTML structure in a template.
  • Pass in data using JavaScript.
  • Let the template engine combine them into the final HTML.

Handlebars is a popular, lightweight template engine that is easy to learn.


✨ Why choose Handlebars?

There are many template engines available. Handlebars offers several useful features:

  • Simple syntax: Variables and logic use {{ }}, making templates easy to read.
  • Minimal template logic: Keeping complex logic out of templates makes them clearer.
  • Extensibility: Custom helpers and partials support reuse.
  • Mature ecosystem: Handlebars has been around for years, with comprehensive documentation and extensive community resources.
Think of Handlebars as a fill-in-the-blanks exercise: write a template with placeholders, fill them with data, and get a complete HTML document.

🚀 Installing Handlebars

Handlebars works in both browsers and Node.js. This course focuses on Node.js.

This course uses Node.js v22.0.0 and Handlebars 4.7.9. Install Handlebars with the following commands:

npm init -y
npm install handlebars@4.7.9

Once installed, require it in your code:

index.js
const Handlebars = require('handlebars');
If you use ES modules ("type": "module"), use import Handlebars from 'handlebars' instead.

📦 Your first Handlebars example

Let's start with a minimal example:

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

// 1. Define the template
const template = '<h1>Hello, {{name}}</h1>';

// 2. Compile the template
const compiled = Handlebars.compile(template);

// 3. Pass in data to get HTML
const html = compiled({ name: 'Ming' });

console.log(html);

Running this produces:

<h1>Hello, Ming</h1>

The process has three steps:

  1. Write a template: Use {{name}} to mark where data should appear.
  2. Compile the template: Handlebars.compile() turns the template string into a reusable function.
  3. Pass in data: Call the compiled function with a data object to get the final HTML.

💡 When to use Handlebars

Handlebars works well for:

  • Rendering pages on the server with Node.js frameworks such as Express or Koa.
  • Generating HTML fragments dynamically in the browser without Vue or React.
  • Generating static HTML for emails and reports.
  • Rendering templates in code generators such as Plop.

If you already use Vue or React, you usually do not need Handlebars, since those frameworks provide their own template or JSX syntax.


❓ Questions and answers

Q1: How does Handlebars template syntax differ from Vue's?

Both use {{ }} for interpolation, but Vue is a reactive framework that updates the DOM when data changes. Handlebars renders data into a string once per call and has no built-in reactivity.

Q2: Does Handlebars only work in Node.js?

No. You can also load it as a script in the browser, although this course focuses on Node.js.

Q3: What does Handlebars.compile() do?

It turns a template string into a function that accepts data and returns rendered HTML. Compile a template once and reuse the function as often as needed.

Q4: Why specify a Handlebars version?

APIs can differ between versions. Pinning the version helps ensure your local results match the course examples.


🧪 Exercise

Create a handlebars-demo project and complete these steps:

  1. Run npm init -y to initialize the project.
  2. Run npm install handlebars@4.7.9 to install the dependency.
  3. Create index.js and use Handlebars to render the following data:
index.js
const data = {
    name: 'Hong',
    age: 20
};

// Write code here to output HTML containing name and age

🎉 You have completed your introduction to Handlebars! Next, we will cover its core syntax, including variables, expressions, and comments.