Handlebars partials: Template reuse and modularity
🎯 Introduction
In this article, you will learn about Handlebars partials. Partials extract repeated HTML into small, reusable templates, much like components. They keep templates clear and maintainable when the same structure appears in several places.
🧱 What are partials?
A partial is a reusable template fragment in Handlebars. Extract shared markup into a partial and reference it wherever you need it.
For example, user cards might share the same structure but display different data:
<div class="user-card">
<h3>Ming</h3>
<p>18 years old</p>
</div>
Writing the same markup ten times for ten users is unnecessary. Instead, define the card structure as a partial and reuse it.
✨ Registering and using partials
1. Register a partial
const Handlebars = require('handlebars');
const cardTemplate = `
<div class="user-card">
<h3>{{name}}</h3>
<p>{{age}} years old</p>
</div>
`;
Handlebars.registerPartial('userCard', cardTemplate);
2. Reference it in a template
{{> userCard}}
3. Complete example
const Handlebars = require('handlebars');
// Register the partial
Handlebars.registerPartial('userCard', `
<div class="user-card">
<h3>{{name}}</h3>
<p>{{age}} years old</p>
</div>
`);
// Main template
const template = `
<div class="user-list">
{{#each users}}
{{> userCard}}
{{/each}}
</div>
`;
const compiled = Handlebars.compile(template);
const html = compiled({
users: [
{ name: 'Ming', age: 18 },
{ name: 'Hong', age: 20 }
]
});
console.log(html);
Output:
<div class="user-list">
<div class="user-card">
<h3>Ming</h3>
<p>18 years old</p>
</div>
<div class="user-card">
<h3>Hong</h3>
<p>20 years old</p>
</div>
</div>
each, its context is the current item.💡 Loading a partial from a file
In real projects, partials usually live in separate files to make them easier to manage.
project/
├── partials/
│ └── user-card.hbs
├── index.js
└── template.hbs
partials/user-card.hbs:
<div class="user-card">
<h3>{{name}}</h3>
<p>{{age}} years old</p>
</div>
// Load the file system module and Handlebars
const fs = require('fs');
const Handlebars = require('handlebars');
// 1. Read the partial template from a file
const userCard = fs.readFileSync('./partials/user-card.hbs', 'utf-8');
// 2. Register the partial as userCard
Handlebars.registerPartial('userCard', userCard);
// 3. Read the main template from a file
const template = fs.readFileSync('./template.hbs', 'utf-8');
// 4. Compile the main template
const compiled = Handlebars.compile(template);
// 5. Pass in data to render HTML
const html = compiled({
users: [
{ name: 'Ming', age: 18 },
{ name: 'Hong', age: 20 }
]
});
// 6. Output the final HTML
console.log(html);
⚡ Passing arguments to a partial
Partials can also accept arguments for more flexible reuse.
{{> userCard name="Ming" age=18}}
The corresponding partial template:
<div class="user-card">
<h3>{{name}}</h3>
<p>{{age}} years old</p>
</div>
name="Ming" passes a string, while age=18 passes a number.🪤 Common pitfalls
Misspelling a partial name
{{> usercard}}
If you register a partial as userCard, references must use exactly the same spelling and capitalization.
Forgetting to register a partial
Using {{> userCard}} without first calling registerPartial causes a runtime error:
Error: The partial userCard could not be found
❓ Questions and answers
Q1: How do partials differ from each?
each handles iteration; partials handle template reuse. They are often used together: each iterates over items, and a partial renders each item's structure.
Q2: Can a partial contain if and each?
Yes. A partial is a Handlebars template fragment and supports all Handlebars syntax.
Q3: Can one partial reference another?
Yes. Partials can be nested as long as they have been registered before rendering.
Q4: How do string and number arguments differ?
name="Ming" passes a string, and age=18 passes a number. Writing age="18" passes a string to the partial instead.
🧪 Exercise
Create partials/comment.hbs and write a main template to render a list of comments:
<!-- Write the comment card partial here -->
const data = {
comments: [
{ author: 'Ming', content: 'Very clear explanation!' },
{ author: 'Hong', content: 'I learned something new. Thanks!' }
]
};
// Register the partial and render the list here
🎉 You have learned how to use Handlebars partials! Next, we will extend templates with custom helpers.
