Classes
Environment
80
▼
Central configuration and template management hub.
The Environment holds all template engine setti…
Environment
80
▼
Central configuration and template management hub.
The Environment holds all template engine settings and provides the primary API for loading and rendering templates. It manages three key concerns:
- Template Loading: Via configurable loaders (filesystem, dict, etc.)
- Compilation Settings: Autoescape, strict undefined handling
- Runtime Context: Filters, tests, and global variables
Strict Mode:
Undefined variables raiseUndefinedErrorinstead of returning empty
string. Catches typos and missing context variables at render time.
>>> env = Environment()
>>> env.from_string("{{ typo_var }}").render()
UndefinedError: Undefined variable 'typo_var' in <template>:1
>>> env.from_string("{{ optional | default('N/A') }}").render()
'N/A'
Caching:
Three cache layers for optimal performance:
-
Bytecode cache (disk): Persistent compiled bytecode via marshal.
Auto-enabled for FileSystemLoader in `__pycache__/kida/`.Current cold-start gain is modest (~7-8% median in`benchmarks/benchmark_cold_start.py`); most startup time is importcost, so lazy imports or pre-compilation are required for largerimprovements. -
Template cache (memory): Compiled Template objects (keyed by name)
-
Fragment cache (memory):
{% cache key %}block outputs>> env.cache_info()
{'template': {'size': 5, 'max_size': 400, 'hits': 100, 'misses': 5},
'fragment': {'size': 12, 'max_size': 1000, 'hits': 50, 'misses': 12},
'bytecode': {'file_count': 10, 'total_bytes': 45000}}
Attributes
| Name | Type | Description |
|---|---|---|
loader |
Loader | None
|
Template source provider (FileSystemLoader, DictLoader, etc.) |
autoescape |
bool | str | Callable[[str | None], bool]
|
HTML auto-escaping. True, False, callable(name) → bool, or a string: "terminal" (enable, ANSI-aware mode), "true" (enable), or "false" (disable). Any other string raises ValueError. |
auto_reload |
bool
|
Check template modification times (default: True) |
strict_none |
bool
|
Fail early on None comparisons during sorting (default: False) |
strict_undefined |
bool
|
— |
jinja2_compat_warnings |
bool
|
— |
preserve_ast |
bool
|
— |
cache_size |
int
|
Maximum compiled templates to cache (default: 400) |
fragment_cache_size |
int
|
Maximum |
fragment_ttl |
float
|
Fragment cache TTL in seconds (default: 300.0) |
bytecode_cache |
BytecodeCache | bool | None
|
Persistent bytecode cache configuration: - None (default): Auto-enabled for FileSystemLoader - False: Explicitly disabled - BytecodeCache instance: Custom cache directory |
static_context |
dict[str, Any] | None
|
— |
_bytecode_cache |
BytecodeCache | None
|
— |
block_start |
str
|
— |
block_end |
str
|
— |
variable_start |
str
|
— |
variable_end |
str
|
— |
comment_start |
str
|
— |
comment_end |
str
|
— |
trim_blocks |
bool
|
— |
lstrip_blocks |
bool
|
— |
max_extends_depth |
int
|
— |
max_include_depth |
int
|
— |
validate_calls |
bool
|
— |
enable_profiling |
bool
|
— |
fstring_coalescing |
bool
|
— |
pure_filters |
set[str]
|
— |
inline_components |
bool
|
— |
optimize_translations |
bool
|
— |
enable_htmx_helpers |
bool
|
— |
extensions |
list[type]
|
— |
terminal_color |
str | None
|
— |
terminal_width |
int | None
|
— |
terminal_unicode |
bool | None
|
— |
ambiguous_width |
int | None
|
— |
globals |
dict[str, Any]
|
Variables available in all templates (includes Python builtins) Thread-Safety: All operations are safe for concurrent use: - Configuration is immutable after |
_filters |
dict[str, Callable[..., Any]]
|
— |
_tests |
dict[str, Callable[..., bool]]
|
— |
_cache |
LRUCache[str, Template]
|
— |
_fragment_cache |
LRUCache[str, str]
|
— |
_template_hashes |
dict[str, str]
|
— |
_template_mtimes |
dict[str, int]
|
— |
_analysis_cache |
dict[str, TemplateMetadata]
|
— |
_structure_manifest_cache |
dict[str, TemplateStructureManifest]
|
— |
_cache_lock |
threading.RLock
|
— |
_terminal_caps |
TerminalCaps | None
|
— |
_extension_instances |
list[Any]
|
— |
_extension_tags |
dict[str, Any]
|
— |
_extension_compilers |
dict[str, Any]
|
— |
_extension_end_keywords |
frozenset[str]
|
— |
_filtered_globals |
dict[str, Any] | None
|
— |
_filtered_globals_source |
dict[str, Any] | None
|
— |
Methods
filters
0
FilterRegistry
▼
Get filters as dict-like registry.
property
filters
0
FilterRegistry
▼
def filters(self) -> FilterRegistry
Returns
FilterRegistry
tests
0
FilterRegistry
▼
Get tests as dict-like registry.
property
tests
0
FilterRegistry
▼
def tests(self) -> FilterRegistry
Returns
FilterRegistry
install_translations
1
▼
Install a gettext translations object.
The object must have gettext() and nget…
install_translations
1
▼
def install_translations(self, translations: Any) -> None
Install a gettext translations object.
The object must have gettext() and ngettext() methods (standard library gettext.GNUTranslations interface).
Parameters
| Name | Type | Description |
|---|---|---|
translations |
— |
install_gettext_callables
2
▼
Install gettext/ngettext functions directly.
These are registered as template …
install_gettext_callables
2
▼
def install_gettext_callables(self, gettext: Any, ngettext: Any) -> None
Install gettext/ngettext functions directly.
These are registered as template globals_ and _n, and
stored as_gettext/_ngettextfor compiler namespace access.
Parameters
| Name | Type | Description |
|---|---|---|
gettext |
— |
|
ngettext |
— |
add_filter
2
▼
Add a filter (copy-on-write).
add_filter
2
▼
def add_filter(self, name: str, func: Callable[..., Any]) -> None
Parameters
| Name | Type | Description |
|---|---|---|
name |
— |
Filter name (used in templates as {{ x | name }}) |
func |
— |
Filter function If func was decorated with :func: |
add_test
2
▼
Add a test (copy-on-write).
add_test
2
▼
def add_test(self, name: str, func: Callable[..., Any]) -> None
Parameters
| Name | Type | Description |
|---|---|---|
name |
— |
Test name (used in templates as {% if x is name %}) |
func |
— |
Test function returning bool |
add_global
2
▼
Add a global variable (copy-on-write).
add_global
2
▼
def add_global(self, name: str, value: Any) -> None
Parameters
| Name | Type | Description |
|---|---|---|
name |
— |
Global name (used in templates as {{ name }}) |
value |
— |
Any value (variable, function, etc.) |
get_filtered_globals
0
dict[str, Any]
▼
Return globals dict with UNDEFINED values removed (cached).
The result is cach…
get_filtered_globals
0
dict[str, Any]
▼
def get_filtered_globals(self) -> dict[str, Any]
Return globals dict with UNDEFINED values removed (cached).
The result is cached, but the cache is automatically refreshed if
the publicglobalsmapping has been mutated or replaced since the
last computation.
Returns
dict[str, Any]
update_filters
1
▼
Add multiple filters at once (copy-on-write).
update_filters
1
▼
def update_filters(self, filters: dict[str, Callable[..., Any]]) -> None
Parameters
| Name | Type | Description |
|---|---|---|
filters |
— |
Dict mapping filter names to functions |
update_tests
1
▼
Add multiple tests at once (copy-on-write).
update_tests
1
▼
def update_tests(self, tests: dict[str, Callable[..., Any]]) -> None
Parameters
| Name | Type | Description |
|---|---|---|
tests |
— |
Dict mapping test names to functions |
get_template
1
Template
▼
Load and cache a template by name.
get_template
1
Template
▼
def get_template(self, name: str) -> Template
Parameters
| Name | Type | Description |
|---|---|---|
name |
— |
Template identifier (e.g., "index.html") |
Returns
Template
Compiled Template object
from_string
3
Template
▼
Compile a template from a string.
from_string
3
Template
▼
def from_string(self, source: str, name: str | None = None, *, static_context: dict[str, Any] | None = None) -> Template
Parameters
| Name | Type | Description |
|---|---|---|
source |
— |
Template source code |
name |
— |
Template name for error messages and bytecode caching. When a None
|
static_context |
— |
Values known at compile time. Expressions that depend only on these values are evaluated during compilation and replaced with constants in the bytecode. This enables near- None
|
Returns
Template
Compiled Template object
clear_template_cache
1
▼
Clear template cache (optional, for external invalidation).
Useful when an ext…
clear_template_cache
1
▼
def clear_template_cache(self, names: list[str] | None = None) -> None
Clear template cache (optional, for external invalidation).
Useful when an external system (e.g., Bengal) detects template changes and wants to force cache invalidation without waiting for hash check.
Parameters
| Name | Type | Description |
|---|---|---|
names |
— |
Specific template names to clear, or None to clear all Default:None
|
get_template_structure
1
TemplateStructureManifes…
▼
Return lightweight manifest with extends/blocks/dependencies.
get_template_structure
1
TemplateStructureManifes…
▼
def get_template_structure(self, name: str) -> TemplateStructureManifest | None
Parameters
| Name | Type | Description |
|---|---|---|
name |
— |
Returns
TemplateStructureManifest | None
render
3
str
▼
Render a template by name with context.
Convenience method combining get_templ…
render
3
str
▼
def render(self, template_name: str, *args: Any, **kwargs: Any) -> str
Render a template by name with context.
Convenience method combining get_template() and render().
Parameters
| Name | Type | Description |
|---|---|---|
template_name |
— |
Template identifier (e.g., "index.html") *args: Single dict of context variables (optional) **kwargs: Context variables as keyword arguments |
*args |
— |
|
**kwargs |
— |
Returns
str
Rendered template as string
render_string
3
str
▼
Compile and render a template string.
Convenience method combining from_string…
render_string
3
str
▼
def render_string(self, source: str, *args: Any, **kwargs: Any) -> str
Compile and render a template string.
Convenience method combining from_string() and render().
Parameters
| Name | Type | Description |
|---|---|---|
source |
— |
Template source code *args: Single dict of context variables (optional) **kwargs: Context variables as keyword arguments |
*args |
— |
|
**kwargs |
— |
Returns
str
Rendered template as string
filter
1
Callable[[Callable[..., …
▼
Decorator to register a filter function.
filter
1
Callable[[Callable[..., …
▼
def filter(self, name: str | None = None) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Parameters
| Name | Type | Description |
|---|---|---|
name |
— |
Filter name (defaults to function name) Default:None
|
Returns
Callable[[Callable[..., Any]], Callable[..., Any]]
Decorator function
test
1
Callable[[Callable[..., …
▼
Decorator to register a test function.
test
1
Callable[[Callable[..., …
▼
def test(self, name: str | None = None) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Parameters
| Name | Type | Description |
|---|---|---|
name |
— |
Test name (defaults to function name) Default:None
|
Returns
Callable[[Callable[..., Any]], Callable[..., Any]]
Decorator function
select_autoescape
1
bool
▼
Determine if autoescape should be enabled for a template.
For ``.kida`` files …
select_autoescape
1
bool
▼
def select_autoescape(self, name: str | None) -> bool
Determine if autoescape should be enabled for a template.
For.kidafiles the inner extension drives the decision: e.g.
page.html.kida → .html → escape, README.md.kida → .md
→ no escape. A bare.kidafile (no inner extension) is treated as
plain text (no escaping).
Parameters
| Name | Type | Description |
|---|---|---|
name |
— |
Template name (may be None for string templates) |
Returns
bool
True if autoescape should be enabled
clear_cache
1
▼
Clear all cached templates and fragments.
Call this to release memory when tem…
clear_cache
1
▼
def clear_cache(self, include_bytecode: bool = False) -> None
Clear all cached templates and fragments.
Call this to release memory when templates are no longer needed, or when template files have been modified and need reloading.
Parameters
| Name | Type | Description |
|---|---|---|
include_bytecode |
— |
Also clear persistent bytecode cache (default: False) Default:False
|
clear_fragment_cache
0
▼
Clear only the fragment cache (keep template cache).
clear_fragment_cache
0
▼
def clear_fragment_cache(self) -> None
clear_bytecode_cache
0
int
▼
Clear persistent bytecode cache.
clear_bytecode_cache
0
int
▼
def clear_bytecode_cache(self) -> int
Returns
int
Number of cache files removed.
cache_info
0
dict[str, Any]
▼
Return cache statistics.
Returns cache statistics for template and fragment ca…
cache_info
0
dict[str, Any]
▼
def cache_info(self) -> dict[str, Any]
Return cache statistics.
Returns cache statistics for template and fragment caches.
Returns
dict[str, Any]
Dict with cache statistics including hit/miss rates.
Internal Methods 5 ▼
__post_init__
0
▼
Initialize derived configuration.
__post_init__
0
▼
def __post_init__(self) -> None
_init_extensions
0
▼
Initialize registered extensions.
_init_extensions
0
▼
def _init_extensions(self) -> None
_resolve_bytecode_cache
0
BytecodeCache | None
▼
Resolve bytecode cache from configuration.
Auto-detection logic:
- If byte…
_resolve_bytecode_cache
0
BytecodeCache | None
▼
def _resolve_bytecode_cache(self) -> BytecodeCache | None
Resolve bytecode cache from configuration.
Auto-detection logic:
- If bytecode_cache is False: disabled
- If bytecode_cache is BytecodeCache: use it
- If bytecode_cache is None and loader is FileSystemLoader:
auto-create cache in first search path's __pycache__/kida/
Returns
BytecodeCache | None
Resolved BytecodeCache or None if disabled/unavailable.
_compile
4
Template
▼
Compile template source to Template object.
Uses bytecode cache when configure…
_compile
4
Template
▼
def _compile(self, source: str, name: str | None, filename: str | None, *, static_context: dict[str, Any] | None = None) -> Template
Compile template source to Template object.
Uses bytecode cache when configured for fast cold-start. Preserves AST for introspection when self.preserve_ast=True (default).
Whenstatic_contextis provided, runs a partial evaluation pass
before compilation to replace static expressions with constants.
Parameters
| Name | Type | Description |
|---|---|---|
source |
— |
|
name |
— |
|
filename |
— |
|
static_context |
— |
Default:None
|
Returns
Template
_is_template_stale
1
bool
▼
Check if a cached template is stale (source changed).
Uses a two-tier check: m…
_is_template_stale
1
bool
▼
def _is_template_stale(self, name: str) -> bool
Check if a cached template is stale (source changed).
Uses a two-tier check: mtime first (cheap stat call), then hash comparison only if mtime changed. This avoids reading and hashing the entire source on every get_template() call when auto_reload=True.
Parameters
| Name | Type | Description |
|---|---|---|
name |
— |
Template identifier |
Returns
bool
True if template source changed, False if unchanged
Functions
_identity_gettext
1
str
▼
Default gettext: return message unchanged.
_identity_gettext
1
str
▼
def _identity_gettext(message: str) -> str
Parameters
| Name | Type | Description |
|---|---|---|
message |
str |
Returns
str
_identity_ngettext
3
str
▼
Default ngettext: select form by count.
_identity_ngettext
3
str
▼
def _identity_ngettext(singular: str, plural: str, n: int) -> str
Parameters
| Name | Type | Description |
|---|---|---|
singular |
str |
|
plural |
str |
|
n |
int |
Returns
str
_consume_global
2
Any
▼
Template global: read a value from the nearest {% provide %} ancestor.
_consume_global
2
Any
▼
def _consume_global(key: str, default: Any = None) -> Any
Parameters
| Name | Type | Description |
|---|---|---|
key |
str |
|
default |
Any |
Default:None
|
Returns
Any
_hash_static_context
1
str | None
▼
Hash static context deterministically for bytecode cache keys.
_hash_static_context
1
str | None
▼
def _hash_static_context(static_context: dict[str, Any] | None) -> str | None
Parameters
| Name | Type | Description |
|---|---|---|
static_context |
dict[str, Any] | None |
Returns
str | None