template-specialize
template-specialize is a command-line tool for rendering Jinja2 templates.
Render a Template File
For example, consider this Markdown name tag template, "nametag.md":
## Hello, my name is
# {{name}}
{% if title is defined %}
### {{title}}
{% endif %}
To render the template, execute template-specialize as follows:
$ template-specialize nametag.md nametag-roy.md -k name 'Roy Hobbs' -k title 'The best there ever was'
Afterward, the output file contains the rendered template:
## Hello, my name is
# Roy Hobbs
### The best there ever was
Render a Directory Template
You can also render directories of templates to an output directory:
$ template-specialize template/ output/ -k name value
Built-In Template Variables
The following template variables are always defined:
now- the current datetime object
Environment Files
template-specialize was originally created to "specialize" web service configuration files for different runtime environments. Environment files are JSON files that allow for the definition of inheritable, structured template configuration values. Consider the following environments file:
{
"base": {
"values": {
"service_name": "my-service"
}
},
"test_base": {
"parents": ["base"],
"values": {
"db_host": "test-db-host"
}
},
// The test environment
"test": {
"parents": ["test_base"],
"values": {
"db_name": "test-db"
}
},
// The live/production environment
"live": {
"parents": ["base"],
"values": {
"db_host": "live-db-host",
"db_name": "live-db"
}
}
}
To render a template file using an environment, specify the environment file (or files) and the environment name with which to render the template:
$ template-specialize config-template.json config.json -c environments.json -e test
To view the template configuration data use the "--dump" argument:
$ template-specialize config-template.json config.json -c environments.json -e test --dump
{
"db_host": "test-db-host",
"db_name": "test-db",
"now": "2026-08-24T12:00:00",
"service_name": "my-service"
}
Renaming and Deleting Output Files
When specializing a template directory, it is sometimes necessary to rename an output file or directory. For example, consider a Python project template with the following structure:
.
|-- README.md
|-- package-name.txt
|-- pyproject.toml
`-- src
|-- __init__.py
|-- package_name
| |-- __init__.py
| `-- package_name.py
`-- tests
|-- __init__.py
`-- test_package_name.py
As part of the specialization, we'd like to rename the "package_name" directory, the "package_name.py" file, and the "test_package_name.py" file to the specialized package name. To accomplish this, we add the "package-name.txt' utility file and call the "template_specialize_rename" Jinja2 extension:
{# Rename template files #}
{% template_specialize_rename 'src/tests/test_package_name.py', 'test_' + package_name + '.py' %}
{% template_specialize_rename 'src/package_name/package_name.py', package_name + '.py' %}
{% template_specialize_rename 'src/package_name', package_name %}
{# Delete the package-name.txt utility template file #}
{% template_specialize_rename 'package-name.txt' %}
First, the "template_specialize_rename" extension is used to rename the package output files and directories. Finally, since we don't want the empty utility file in the output, we delete it using the "template_specialize_rename" extension with no second argument. Here's an example usage of our Python project template:
$ template-specialize python-package my-package -k package_name my_package
This command produces the following specialized template output with appropriately named package source directory and source files:
.
|-- README.md
|-- pyproject.toml
`-- src
|-- __init__.py
|-- my_package
| |-- __init__.py
| `-- my_package.py
`-- tests
|-- __init__.py
`-- test_my_package.py
AWS Parameter Store
template-specialize can retrieve template values from AWS Parameter Store using botocore.
Here's an example of a JSON configuration file with a Parameter Store secret:
{
"my_secret": {% filter tojson %}{% aws_parameter_store 'parameter-name' %}{% endfilter %}
}
botocore is usually configured using environment variables.
Usage
usage: template-specialize [-h] [-i PATH] [-c FILE] [-e ENV] [-k KEY VALUE]
[--dump]
SRC DST
positional arguments:
SRC the source template file or directory
DST the destination file or directory
options:
-h, --help show this help message and exit
-i PATH add an include search path
-c FILE the environment files
-e ENV the environment name
-k, --key KEY VALUE add a template key and value
--dump dump the template variables
Development
This package is developed using python-build. It was started using python-template as follows:
template-specialize python-template/template/ template-specialize/ -k package template-specialize -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs' -k noapi 1
Metadata
Release files for template-specialize 1.6.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| template_specialize-1.6.4.tar.gz | 10.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| template_specialize-1.6.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.5 kB
Release files / template_specialize-1.6.4.tar.gz
| Download URL | template_specialize-1.6.4.tar.gz |
|---|---|
| Size | 10.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9ffa1e07ee2f370571ea263e8e1f91947d240304c13cfd32b3d92fff03889a63
|
|
BLAKE2b-256 checksum How to use checksums |
53d2b85d6ce2f3259c2d9df6c52697c09e90133d70abd307701fc1ac388e8fee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / template_specialize-1.6.4-py3-none-any.whl
| Download URL | template_specialize-1.6.4-py3-none-any.whl |
|---|---|
| Size | 9.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2757479fc543eb8c56eb4060235195760389e7bd13119511861ac4904d53e69f
|
|
BLAKE2b-256 checksum How to use checksums |
9cf06ccd383570b7082ed550931ac1b509713e601b6e3e3f53bbf4255dbf4ce6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|