Module
environment
Kida Environment — central configuration and template management.
The Environment is the central hub for all template operations:
- Configuration: Delimiters, autoescape, strict mode
- Loading: File system, dict, or custom loaders
- Caching: LRU caches for templates and fragments
- Extensibility: Custom filters, tests, and global variables
Thread-Safety:
Environment is designed for concurrent access:
- Copy-on-write for
add_filter(),add_test(),add_global() - Lock-free LRU cache with atomic pointer swaps
- Immutable configuration after construction
Public API:
Environment: Central configuration classFileSystemLoader: Load templates from directoriesDictLoader: Load templates from memory (testing)FilterRegistry: Dict-like interface for filters/testsLoader,Filter,Test: Protocol definitions
Exceptions:
TemplateError: Base exception for all template errorsTemplateNotFoundError: Template not found by loaderTemplateSyntaxError: Parse-time syntax errorUndefinedError: Undefined variable in strict mode
Example:
>>> from kida import Environment, FileSystemLoader
>>> env = Environment(
... loader=FileSystemLoader("templates/"),
... autoescape=True,
... strict=True,
... )
>>> env.add_filter("upper", str.upper)
>>> template = env.get_template("page.html")
>>> template.render(title="Hello")