Template Tasks with Handlebars

The template task type supports powerful templating capabilities using Handlebars, enabling you to generate files from templates with advanced features like conditionals, loops, and helpers.

Overview

Template tasks can use two different templating approaches:

  1. Simple Interpolation (default): Basic `` replacement for inline templates and non-.hbs files
  2. Handlebars: Full Handlebars templating engine for files with .hbs extension

Templates can be defined in two ways:

  1. Inline: Template string directly in the task configuration (simple interpolation only)
  2. File-based: External template file (relative to project root or template source). .hbs files automatically use Handlebars

Note: When using configuration inheritance with remote configurations, templateFile paths are automatically resolved relative to the remote configuration’s location. This allows remote configurations to reference their own template files hosted alongside them.

Configuration Options

Basic Options

Important: You must specify either template OR templateFile, but not both.

Automatic Handlebars Detection

Handlebars is automatically enabled for any template file ending in .hbs. No configuration needed!

File-based Handlebars Example

Template file (templates/readme.hbs):

#


>

is a modern TypeScript project.


  ## πŸ‘₯ Author

  



  ## ✨ Features

  
    -
    
  


## πŸ“„ License MIT

Task configuration:

{
  "id": "clean-readme",
  "name": "Clean README",
  "description": "Create a fresh README for the new project",
  "required": true,
  "enabled": true,
  "type": "write",
  "config": {
    "file": "README.md",
    "templateFile": "templates/readme.hbs"
  }
}

Simple Interpolation (Default)

When using inline templates or template files that don’t end in .hbs, simple `` replacement is used.

Inline Template Example

{
  "id": "simple-readme",
  "name": "Simple README",
  "type": "write",
  "config": {
    "file": "README.md",
    "template": "# \n\nAuthor: \nRepository: "
  }
}

File-based Simple Template Example

Template file (templates/simple-readme.txt):

# 

Author: 
Repository: 

Task configuration:

{
  "id": "simple-readme-file",
  "name": "Simple README from File",
  "type": "write",
  "config": {
    "file": "README.md",
    "templateFile": "templates/simple-readme.txt"
  }
}

Handlebars Features

Conditionals


  Author:
  



  This is a public package

Loops

## Packages


  - ****:
  

Else Blocks


  

  No description available

Comments


Built-in Helpers

Handlebars includes many built-in helpers:

See Handlebars documentation for complete details.

Available Variables

All configuration variables from your InitConfig are available in templates:

Best Practices

When to Use Simple vs. Handlebars

Use Simple Interpolation when:

Use Handlebars when:

Inline vs. File-based Templates

Use Inline Templates when:

Use File-based Templates when:

File Organization

Recommended structure for template files:

project-root/
β”œβ”€β”€ .scaffoldfy/
β”‚   └── templates/
β”‚       β”œβ”€β”€ readme.hbs          # Handlebars template
β”‚       β”œβ”€β”€ package-json.hbs    # Handlebars template
β”‚       β”œβ”€β”€ license.txt         # Simple template
β”‚       └── contributing.md     # Simple template
└── config-tasks.json

Complete Example

Task configuration (config-tasks.json):

{
  "tasks": [
    {
      "id": "clean-readme",
      "name": "Generate README",
      "description": "Create README using Handlebars template",
      "required": true,
      "enabled": true,
      "type": "write",
      "config": {
        "file": "README.md",
        "templateFile": ".scaffoldfy/templates/readme.hbs"
      }
    },
    {
      "id": "update-package-json",
      "name": "Update package.json",
      "description": "Generate package.json from simple template",
      "required": true,
      "enabled": true,
      "type": "write",
      "config": {
        "file": "package.json",
        "templateFile": ".scaffoldfy/templates/package-json.hbs"
      }
    },
    {
      "id": "inline-license",
      "name": "Create LICENSE",
      "description": "Create simple license file",
      "required": false,
      "enabled": true,
      "type": "write",
      "config": {
        "file": "LICENSE",
        "template": "MIT License\n\nCopyright (c) \n\nPermission is hereby granted..."
      }
    }
  ]
}

Handlebars template (.scaffoldfy/templates/readme.hbs):

#



  >
  

  > A modern TypeScript project


## πŸš€ Quick Start ```sh # Install dependencies pnpm install # Build pnpm build # Test pnpm
test

✨ Features

πŸ‘₯ Author

πŸ“„ License

MIT


## Error Handling

### Missing Template File

If a `templateFile` is specified but doesn't exist, the task will throw an error:

βœ— Failed to read template file: templates/missing.hbs βœ— Error: Template file not found: templates/missing.hbs


### Invalid Handlebars Syntax

If Handlebars syntax is invalid in a `.hbs` file, you'll get a compilation error:

βœ— Failed to compile Handlebars template βœ— Error: Parse error on line 5: …


### Both template and templateFile

You cannot specify both options:

βœ— Template task cannot have both β€œtemplate” and β€œtemplateFile” specified. Use one or the other.


## Migration from Previous Versions

### From useHandlebars flag

**Before (with flag)**:

```json
{
  "templateFile": "readme.hbs",
  "useHandlebars": true
}

After (automatic):

{
  "templateFile": "readme.hbs"
}

From inline Handlebars

Before (inline Handlebars):

{
  "template": "# \nAuthor: ",
  "useHandlebars": true
}

After (file-based only):

{
  "templateFile": "readme.hbs"
}

See Also