Skip to main content

A simple templates loader

Project description

Stencils — Lightweight Template Loader for Python

PyPI - Version PyPI - Python Version GitHub License PyPI - Wheel PyPI - Downloads

Overview

Stencils is a minimal, zero-dependency Python library for managing text templates embedded in files.
It’s ideal for projects that need small, human-readable, and reusable template snippets (e.g., config fragments, codegen templates, or documentation blocks).

The library parses .tmpl (or any text) files containing named template blocks, resolves duplicates, and provides a clean dictionary-like interface for use in your code.

Installation

pip install stencils

Template Format

A single file can contain multiple named template blocks.
Each block must start with a line of the form:

===== TEMPLATE:<name> =====

and must end with:

===== /TEMPLATE =====

Example:

===== TEMPLATE:greeting =====
Hello {name}!
Welcome to {place}.
===== /TEMPLATE =====

===== TEMPLATE:farewell =====
Goodbye {name}, see you soon!
===== /TEMPLATE =====

Usage

Loading Templates

from stencils import load, render

templates = load("templates/example.tmpl")

print(templates["greeting"])
# Output:
# Hello {name}!
# Welcome to {place}.

You can also load multiple files, directories, or patterns:

templates = load(["./templates", "common/*.tmpl"])

Each .tmpl file is scanned for TEMPLATE: blocks.
Duplicate keys across files will raise TemplateConflictError unless the templates are identical.

Rendering Templates

You can render any loaded template with a dictionary of values:

text = render(templates["greeting"], {"name": "Alice", "place": "Wonderland"})
print(text)

Output:

Hello Alice!
Welcome to Wonderland.

Missing placeholders default to empty strings, so this is safe:

render(templates["farewell"], {})
# -> "Goodbye , see you soon!"

Controlling Missing Placeholders

You can control how missing placeholders are handled with the on_missing option.

render(template, data, on_missing="empty")

Available options:

Option Description Example Output
"empty" (default) Replace missing keys with an empty string. "Hello , welcome!"
"keep" Keep the original placeholder text {key} if it’s missing. "Hello {name}, welcome!"
"error" Raise a KeyError for any missing placeholder. Raises KeyError: 'name'

Errors

Exception Description
TemplateConflictError Raised when a template key appears in multiple files with different content.
TemplateParseError Raised on malformed templates (e.g., unclosed or nested blocks, missing start/end).

API Reference

load(targets)

Load one or more template sources (files, directories, or globs).

Args:

  • targets: Path, string, or iterable of paths/globs.

Returns:
A dict[str, str] mapping template names to their contents.
Each template is also available under an alias prefixed with a colon (:{name}).

render(template, data)

Render a template using Python’s str.format_map, defaulting missing keys to empty strings.

Args:

  • template: Template string.
  • data: Dict of substitutions.

Returns: Rendered string.

Example Project Structure

myproject/
 ├── templates/
 │    ├── main.tmpl
 │    └── footer.tmpl
 └── app.py
# app.py
from stencils import load, render

templates = load("templates")

output = render(templates["main"], {"title": "Hello World"})
print(output)

🧾 License

This project is licensed under the MIT License — see the LICENSE file for details.

© 2025 Ioannis D. (devcoons)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stencils-0.1.6.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stencils-0.1.6-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file stencils-0.1.6.tar.gz.

File metadata

  • Download URL: stencils-0.1.6.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for stencils-0.1.6.tar.gz
Algorithm Hash digest
SHA256 99824d95abb6c274a7856c8e6a8940caeb0d01ee9ec38ed0a959d1e6071be32e
MD5 a5a812aabb5a69c90468c748d62e1a88
BLAKE2b-256 384ddf78c8729c6676bbacc1835d045b7dbaa3366388430d6c661db2c376c7c2

See more details on using hashes here.

File details

Details for the file stencils-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: stencils-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for stencils-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 155d8a05bc4f8e72b87ae392d38689a906f6463b69657e34bf8347ca4d91e166
MD5 686b996c19369a2f4965fb21528801f7
BLAKE2b-256 b59a90b38bd3891fe004eda495e325e3be4cd494dee6c1eae3ce1280181b465c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page