Getting started with Handlebars: What is a template engine?
🎯 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:
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.
🚀 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:
const Handlebars = require('handlebars');
"type": "module"), use import Handlebars from 'handlebars' instead.📦 Your first Handlebars example
Let's start with a minimal example:
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:
- Write a template: Use
{{name}}to mark where data should appear. - Compile the template:
Handlebars.compile()turns the template string into a reusable function. - 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:
- Run
npm init -yto initialize the project. - Run
npm install handlebars@4.7.9to install the dependency. - Create
index.jsand use Handlebars to render the following data:
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.
