Templates in Plop: File Organization and Variables
🎯 Introduction
In this article, you will learn where to store Plop templates, how to name them, and how to pass command-line input into your templates.
This course does not repeat the Handlebars syntax lessons. If you are unfamiliar with {{variable}}, {{#if}}, or {{#each}}, start with the earlier Handlebars course.
🧱 Where to Store Plop Templates
Plop templates are usually kept together in a plop-templates/ directory for easier management and maintenance.
For a simple project, use a structure like this:
plop-templates/
├── component.hbs
├── page.hbs
├── api.hbs
└── util.hbs
If a generator needs multiple template files, create a subdirectory:
plop-templates/
└── component/
├── index.vue
├── index.js
└── style.scss
.hbs, the standard Handlebars extension. Plop does not require this extension, but it makes template files easy to recognize.✨ Variables Available in Templates
Answers collected by prompts automatically become template variables. Each variable uses the question object's name field.
For example, consider this prompt:
{
type: 'input',
name: 'name',
message: 'Enter the component name'
}
Use the answer in a template like this:
<div>{{name}}</div>
A confirm prompt returns true or false, which you can use directly with {{#if}}:
{{#if hasStyle}}
<style scoped>
.{{kebabCase name}} { }
</style>
{{/if}}
A checkbox prompt returns an array, which you can iterate over with {{#each}}:
{{#each features}}
<li>{{this}}</li>
{{/each}}
🛠 Built-in Case Conversion Helpers
Plop provides useful built-in helpers for converting one input into different naming styles. These Plop-specific extensions were not covered in the Handlebars course.
| Helper | Description | Input: userInfo |
|---|---|---|
camelCase | camelCase | userInfo |
pascalCase | PascalCase | UserInfo |
kebabCase | kebab-case | user-info |
snakeCase | snake_case | user_info |
constantCase | UPPER_SNAKE_CASE | USER_INFO |
lowerCase | All lowercase | userinfo |
upperCase | All uppercase | USERINFO |
Example:
<template>
<div class="{{kebabCase name}}">
{{pascalCase name}} component
</div>
</template>
With userInfo as the input, this renders as:
<template>
<div class="user-info">
UserInfo component
</div>
</template>
pascalCase for component names and filenames, kebabCase for class names and CSS selectors, and camelCase for variable and function names.💡 Tips for Organizing Templates
Give each generator its own template file or directory, named after the generator so it is easy to find:
plop-templates/
├── component.hbs # component generator
├── page.hbs # page generator
├── page.scss.hbs # Style template for the page generator
└── api.hbs # api generator
Avoid combining all templates in one file. Clear organization makes future maintenance easier.
❓ Questions and Answers
Q1: Must templates be stored in plop-templates/?
No, but it is a community convention. A shared directory helps teammates recognize template files at a glance.
Q2: What happens if a template uses a name that is not defined in prompts?
Handlebars renders it as an empty string without throwing an error.
Q3: Were Plop's case conversion helpers covered in the Handlebars course?
No. Plop adds these helpers to make it easy to generate filenames and class names in different naming styles.
Q4: Can templates use custom helpers?
Yes. Custom helpers registered with plop.setHelper() work just like built-in helpers in templates. We will cover them in the next article.
🧪 Exercise
Given userAvatar as the answer to the name prompt, complete the template so the output includes both a PascalCase component name and a kebab-case class name:
<template>
<div class="{{kebabCase name}}">
<!-- Output the name in PascalCase here -->
</div>
</template>
🎉 You now know how to organize Plop templates and pass variables to them! Next, we will learn how to extend Plop with helpers.
