Classes
TemplateEngine
Template engine for rendering pages with Jinja2 templates.
Provides Jinja2 template rendering with…
TemplateEngine
Template engine for rendering pages with Jinja2 templates.
Provides Jinja2 template rendering with theme inheritance, template function registration, asset manifest access, and optional template profiling. Manages template discovery through theme chains and provides cache-busting via asset manifest.
Creation:
Direct instantiation: TemplateEngine(site, profile_templates=False)
- Created by RenderingPipeline for template rendering
- Requires Site instance with theme configuration
- Optional profiling enabled via profile_templates flag
MenuHelpersMixin,ManifestHelpersMixin,AssetURLMixinAttributes
| Name | Type | Description |
|---|---|---|
site |
— | Site instance with theme and configuration |
template_dirs |
— | List of template directories (populated during init) |
env |
— | Jinja2 Environment instance |
_profile_templates |
— | Whether template profiling is enabled |
_profiler |
— | Optional TemplateProfiler for performance analysis |
_dependency_tracker |
— | Optional DependencyTracker (set by RenderingPipeline) |
_asset_manifest_cache |
— | Cached asset manifest entries |
Relationships |
— |
|
Methods 4
render
Render a template with the given context.
render
def render(self, template_name: str, context: dict[str, Any]) -> str
Render a template with the given context.
Parameters 2
template_name |
str |
Name of the template file |
context |
dict[str, Any] |
Template context variables |
Returns
Rendered HTMLstr
—
get_template_profile
Get template profiling report.
get_template_profile
def get_template_profile(self) -> dict[str, Any] | None
Get template profiling report.
Returns
Dictionary with template and function timing statistics,
or None if profiling is not enabled.dict[str, Any] | None
—
render_string
Render a template string with the given context.
render_string
def render_string(self, template_string: str, context: dict[str, Any]) -> str
Render a template string with the given context.
Parameters 2
template_string |
str |
Template content as string |
context |
dict[str, Any] |
Template context variables |
Returns
Rendered HTMLstr
—
validate_templates
Validate syntax of all templates in the search path.
Proactively checks all di…
validate_templates
def validate_templates(self, include_patterns: list[str] | None = None) -> list[Any]
Validate syntax of all templates in the search path.
Proactively checks all discoverable templates for syntax errors by attempting to compile them through Jinja2. This catches errors before rendering, providing early feedback during builds.
Parameters 1
include_patterns |
list[str] | None |
Optional list of glob patterns to limit validation (e.g., ["page.html", "partials/*.html"]). If None, validates all .html and .xml templates. |
Returns
List of TemplateRenderError for any templates with syntax errors.
Empty list if all templates are valid.list[Any]
—
Internal Methods 6
__init__
Initialize the template engine.
__init__
def __init__(self, site: Any, profile_templates: bool = False) -> None
Initialize the template engine.
Parameters 2
site |
Any |
Site instance |
profile_templates |
bool |
Enable template profiling for performance analysis |
_url_for
Generate URL for a page with base URL support.
_url_for
def _url_for(self, page: Any) -> str
Generate URL for a page with base URL support.
Parameters 1
page |
Any |
Page object |
Returns
URL path with base URL prefix if configuredstr
—
_with_baseurl
Apply base URL prefix to a path.
_with_baseurl
def _with_baseurl(self, path: str) -> str
Apply base URL prefix to a path.
Parameters 1
path |
str |
Relative path starting with '/' |
Returns
Path with base URL prefixstr
—
_find_template_path
Find the full path to a template file.
_find_template_path
def _find_template_path(self, template_name: str) -> Path | None
Find the full path to a template file.
Parameters 1
template_name |
str |
Name of the template |
Returns
Full path to template file, or None if not foundPath | None
—
_resolve_theme_chain
Resolve theme inheritance chain starting from the active theme.
Compatibility …
_resolve_theme_chain
def _resolve_theme_chain(self, active_theme: str | None) -> list[str]
Resolve theme inheritance chain starting from the active theme.
Compatibility method for tests. Delegates to resolve_theme_chain function.
Parameters 1
active_theme |
str | None |
Active theme name |
Returns
List of theme names in inheritance orderlist[str]
—
_read_theme_extends
Read theme.toml for 'extends' value.
Compatibility method for tests. Delegates…
_read_theme_extends
def _read_theme_extends(self, theme_name: str) -> str | None
Read theme.toml for 'extends' value.
Compatibility method for tests. Delegates to read_theme_extends function.
Parameters 1
theme_name |
str |
Theme name to look up |
Returns
Parent theme name if extends is set, None otherwisestr | None
—