This example demonstrates how to define variables in your template and use them to customize files during project initialization.
In your ~/.pt/config.yaml, add a variables section to your template and set substitute_variables: true in the copy_files entries.
templates:
node_web_app:
description: "A standard Node.js web application"
variables:
- name: "project_name"
prompt: "What is the project name?"
default: "my-app"
- name: "author_name"
prompt: "Who is the author?"
required: true
copy_files:
- src: "templates/package.json.tmpl"
dest: "package.json"
substitute_variables: true
- src: "templates/README.md.tmpl"
dest: "README.md"
substitute_variables: trueTip
Automatic detection (v0.16+): You no longer need to manually define the variables section. During pt learn or pt update, the tool will automatically detect {{ variable_name }} placeholders in your files and add them to the configuration for you.
Create the source files in your template directory using {{variable_name}} placeholders.
templates/package.json.tmpl:
{
"name": "{{project_name}}",
"version": "1.0.0",
"description": "Project created by pt-cli",
"author": "{{author_name}}",
"license": "MIT"
}templates/README.md.tmpl:
# {{project_name}}
Created by {{author_name}} using the Node Web App template.
## Getting Started
1. npm install
2. npm run devWhen you run pt init node_web_app my-new-project, the CLI will prompt you:
? What is the project name? (my-app) my-awesome-service
? Who is the author? Gary Ritchie
The resulting package.json in my-new-project/ will be:
{
"name": "my-awesome-service",
"version": "1.0.0",
"description": "Project created by pt-cli",
"author": "Gary Ritchie",
"license": "MIT"
}Starting with v0.36.0, you can use nested variables for more complex configurations. Create a .env file in your project directory or parent directory:
# .env file in parent directory
prefix='rst_'
project=MyProjectThen use these variables in your template files:
templates/README.md.tmpl:
# {{prefix}}{{project}}
This is a nested variable example where:
- prefix = 'rst_'
- project = 'MyProject'
- Result: 'rst_MyProject'Even more complex nesting:
# .env file
prefix='app_{{ env }}'
env=prod
project=MyApp
version=2.0Then in your template:
{
"name": "{{prefix}}_{{project}}",
"version": "{{version}}"
}This will resolve to:
{
"name": "app_prod_MyApp",
"version": "2.0"
}How it works:
ptscans parent directories for.envfiles- Loads variables from
.envas defaults - Expands nested placeholders iteratively (up to 10 passes)
- Resolves circular references gracefully
Example with nested placeholders:
# .env file
template_path='docs/{{ project }}'
project=wikiResult: template_path becomes docs/wiki
Important: Missing nested variables remain as {{ variable }} placeholders (with preserved whitespace) to help identify configuration issues.
Pass variables non-interactively with --vars:
pt init node_web_app my-project --yes --vars project_name=my-service,author_name="Jane Doe"Or use a .env file in the destination directory for persistent defaults.