wijjit.core.renderer.Renderer
- class wijjit.core.renderer.Renderer(template_dir=None, autoescape=False, auto_reload=False, *, strict_undefined=False)[source]
Template renderer using Jinja2.
This class manages Jinja2 template rendering with support for both string templates and file-based templates.
- Parameters:
template_dir (
str, optional) – Directory containing template filesautoescape (
bool, optional) – Whether to enable autoescaping (default: False for terminal output)auto_reload (
bool, optional) – Whether Jinja2 reloads file templates when they change on disk (default: False). Useful during development; maps to theTEMPLATE_AUTO_RELOADconfig key.strict_undefined (
bool,optional keyword-only) – Whether the Jinja2 environment usesjinja2.StrictUndefined(default: False). In strict mode, referencing an undefined template name (e.g.{{ typo_var }}) raisesjinja2.UndefinedErrorat render time, surfacing typos instead of silently rendering an empty string. The production default is lenient (jinja2.Undefined) so that one bad key cannot crash a running TUI; strict mode is enabled for development (DEBUG) and by the devtools validator.
- Variables:
env (
jinja2.Environment) – The Jinja2 environmentstrict_undefined (
bool) – Whether undefined template names raise at render time._string_templates (
dict) – Cache of string templates
- __init__(template_dir=None, autoescape=False, auto_reload=False, *, strict_undefined=False)[source]
Methods
__init__([template_dir, autoescape, ...])add_filter(name, func)Add a custom filter to the Jinja2 environment.
add_global(name, value)Add a global variable to the Jinja2 environment.
Clear the template cache.
Clear the reconciler element cache and VNode tree.
composite_overlays(base_output, ...[, ...])Composite overlay elements on top of base output.
Get the last rendered buffer as plain text (for testing).
Return the set of Jinja2 tag names registered by Wijjit extensions.
Get the last rendered buffer for external diff rendering.
Discard the cached on-screen buffers so the next render is full.
render_file(template_name[, context])Render a template from a file.
render_string(template_string[, context])Render a template from a string.
render_with_layout([template_string, ...])Render a template with layout engine support.
- get_extension_tag_names()[source]
Return the set of Jinja2 tag names registered by Wijjit extensions.
Useful for callers that need to determine whether a template uses any Wijjit-specific tag (and therefore must be routed through the layout pipeline rather than plain Jinja2 string rendering).
- Returns:
Every tag name registered by the Wijjit extension classes (e.g.
"frame","textinput","button").- Return type:
set[str]
- render_file(template_name, context=None)[source]
Render a template from a file.
- Parameters:
- Returns:
The rendered template output
- Return type:
- Raises:
jinja2.TemplateNotFound – If the template file doesn’t exist
- add_filter(name, func)[source]
Add a custom filter to the Jinja2 environment.
- Parameters:
name (
str) – Name of the filterfunc (
Callable) – Filter function
- Return type:
None
- add_global(name, value)[source]
Add a global variable to the Jinja2 environment.
- Parameters:
name (
str) – Name of the global variablevalue (
Any) – Value of the global variable
- Return type:
None
- render_with_layout(template_string='', context=None, width=None, height=None, template_name=None, allow_incremental=False)[source]
Render a template with layout engine support.
This method handles the full pipeline for layout-based templates: 1. Create layout context 2. Render template (building layout tree) 3. Run layout engine to calculate positions 4. Compose final output from positioned elements
- Parameters:
template_string (
str, optional) – The template string to render (use this OR template_name)context (
dict, optional) – Context variables for template renderingwidth (
int, optional) – Available width (default: terminal width)height (
int, optional) – Available height (default: terminal height)template_name (
str, optional) – Name of template file to load from template_dir (use this OR template_string)allow_incremental (bool)
- Returns:
Rendered output, list of elements with bounds, and layout context containing overlay information from template tags
- Return type:
- get_last_buffer()[source]
Get the last rendered buffer for external diff rendering.
- Returns:
Last rendered buffer, or None if not yet rendered
- Return type:
ScreenBufferorNone
- invalidate_display()[source]
Discard the cached on-screen buffers so the next render is full.
Diff rendering compares the new frame against
_last_displayed_bufferand emits only the cells that changed, so bytes written to the terminal out-of-band (foreignstdout, a stray traceback, a subprocess) are invisible to the differ and persist on screen. Clearing the cached buffers forces the nextrender_diffdown the_full_renderpath - a screen clear plus a complete repaint - which overwrites any such bytes. Both the base and displayed buffers are cleared so overlay compositing also starts from a clean slate.- Return type:
None
- get_buffer_as_text()[source]
Get the last rendered buffer as plain text (for testing).
- Returns:
Plain text representation of the last buffer
- Return type:
Notes
This is primarily used for test assertions where you want to check for visible text content without parsing ANSI escape sequences. Returns empty string if no buffer has been rendered yet.
Examples
>>> renderer = Renderer() >>> output, _, _ = renderer.render_with_layout(template, 80, 24) >>> text = renderer.get_buffer_as_text() >>> 'Welcome' in text True
- composite_overlays(base_output, overlay_elements, width, height, apply_dimming=False, dim_factor=0.6, force_full_redraw=False)[source]
Composite overlay elements on top of base output.
This method renders overlays on top of the base UI output, optionally applying backdrop dimming for modal effects.
- Parameters:
base_output (
str) – Base UI output (ANSI string or newline-separated lines)overlay_elements (
listofElement) – Overlay elements with assigned bounds (in z-order, lowest first)width (
int) – Screen widthheight (
int) – Screen heightapply_dimming (
bool) – Whether to dim the base output (for modal backdrops)dim_factor (
float) – Dimming intensity (0.0 = black, 1.0 = original). Default is 0.6.force_full_redraw (
bool) – Force full screen redraw instead of diff rendering
- Returns:
Composited output with overlays on top
- Return type:
Notes
Overlays are rendered in the order provided (first = bottom, last = top). If apply_dimming is True, the base output is dimmed before overlays are composited.
Overlays are composited directly on the cell buffer for efficient rendering.