Extending Plop with Helpers
🎯 Introduction
In this article, you will learn about Plop's built-in helpers and how to add your own with plop.setHelper().
For the basics of Handlebars helpers, including what they are and how to call them in templates, see the earlier Handlebars course. This article focuses on Plop-specific features.
🧱 Common Built-in Plop Helpers
Besides the case conversion helpers introduced in the previous article (camelCase, pascalCase, kebabCase, and others), Plop provides several helpers for project development.
pkg
Reads values from the current project's package.json.
Project name: {{pkg 'name'}}
Version: {{pkg 'version'}}
raw
Outputs raw content without Handlebars processing it.
{{raw}}{{name}}{{/raw}}
include
Includes another template fragment.
{{include 'header'}}
date
Outputs the current date. The default format may vary by environment, so a custom helper with a fixed format is preferable in real projects.
<!-- Created on {{date}} -->
pkg, raw, include, and date, and consult the documentation when needed.✨ Defining Custom Helpers with plop.setHelper
Register custom helpers with plop.setHelper(), a Plop-specific API.
Example: Generating a Date in a Fixed Format
export default function (plop) {
plop.setHelper('now', function () {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
});
}
Use it in a template:
<!--
Component: {{pascalCase name}}
Created on: {{now}}
-->
Example: Accepting Multiple Arguments
plop.setHelper('prefix', function (text, pre) {
return pre + '-' + text;
});
Use it in a template:
<div class="{{prefix name 'el'}}">{{name}}</div>
With button as the input, this renders as:
<div class="el-button">button</div>
❓ Questions and Answers
Q1: Can a custom helper override a built-in helper?
Yes, if they share a name. Choose project-specific names for custom helpers to avoid conflicts.
Q2: How does Plop's setHelper differ from Handlebars' registerHelper?
They serve similar purposes, but setHelper is Plop's wrapper API. Use plop.setHelper() in Plop projects to register the helper with Plop's Handlebars instance.
Q3: What does {{raw}} do?
Content between {{raw}} and {{/raw}} is not processed by Handlebars. This is useful for outputting strings containing {{ }}. See the Handlebars course for details.
Q4: Can custom helpers return values other than strings?
Returning strings is recommended. Handlebars may convert other types, which can produce unexpected results.
🧪 Exercise
Write a custom helper named suffix that accepts two arguments and appends the second to the first, separated by a hyphen.
For example, {{suffix name 'item'}} should produce user-item when the input is user.
plop.setHelper('suffix', function (text, suf) {
// Complete the code here
});
🎉 You now know how to extend Plop with helpers! Next, we will bring everything together in a complete practical example.
