wijjit.core.view_router.ViewRouter
- class wijjit.core.view_router.ViewRouter(app)[source]
Manages view registration and navigation.
The view router handles: - View registration via decorator - View lookup and caching - View navigation with parameters - Lifecycle hook execution (on_enter, on_exit) - Template initialization
- Parameters:
app (
Wijjit) – Reference to the main application- Variables:
- __init__(app)[source]
Initialize the view router.
- Parameters:
app (
Wijjit) – Reference to the main application- Return type:
None
Methods
__init__(app)Initialize the view router.
evaluate_render(view_config[, params])Resolve the template + context to render for the current frame.
get_view(view_name)Get a view configuration by name.
navigate(view_name[, params])Navigate to a different view.
view_decorator(name[, default, on_enter, ...])Create a decorator to register a view.
- view_decorator(name, default=False, on_enter=None, on_exit=None)[source]
Create a decorator to register a view.
The decorated function returns what to render - preferably via
wijjit.render_template_string()/wijjit.render_template(), which package the template and its live context. A synchronous view function is re-invoked on every render, so any context it computes stays current. The legacy{"template": ..., "data": {...}}dict and a bare template string are also accepted.- Parameters:
name (
str) – Unique name for this viewdefault (
bool) – Whether this is the default view (default: False)on_enter (
callable, optional) – Hook called when navigating to this view. Takes precedence over anon_enterkey in a legacy dict return.on_exit (
callable, optional) – Hook called when navigating away from this view. Takes precedence over anon_exitkey in a legacy dict return.
- Returns:
Decorator function
- Return type:
Callable
Examples
Inline template (preferred):
>>> @app.view("main", default=True, on_enter=setup) ... def main_view(): ... return render_template_string("Hello, {{ name }}!", name="World")
Load from a file:
>>> @app.view("dashboard") ... def dashboard_view(): ... return render_template("dashboard.wij.j2", stats=get_stats())
- evaluate_render(view_config, params=None)[source]
Resolve the template + context to render for the current frame.
Synchronous views are re-invoked on every render so any context they compute stays live (the whole point of the Flask-style API). Async views cannot be awaited from the synchronous render path, so they fall back to the template +
datacallable captured at initialization.- Parameters:
view_config (
ViewConfig) – The view to evaluate (already initialized).params (
dict, optional) – Navigation parameters passed to the view/data callable.
- Returns:
The template (inline or file) and context for this render.
- Return type:
RenderedView
Navigate to a different view.
Automatically detects async context and handles both sync and async view functions and lifecycle hooks. When called from an async context (which is typical in Wijjit applications), navigation is scheduled as an async task to support async hooks.
- Parameters:
- Raises:
ValueError – If view_name doesn’t exist
- Return type:
None
Notes
This method replaces the previous separate navigate() and navigate_async() methods. It auto-detects the execution context: - If running in async event loop: schedules async navigation task - If running in sync context: performs synchronous navigation
The async path supports both sync and async lifecycle hooks (on_enter, on_exit) and view functions, while the sync path only supports sync hooks.
- get_view(view_name)[source]
Get a view configuration by name.
- Parameters:
view_name (
str) – Name of the view to retrieve- Returns:
View configuration, or None if not found
- Return type:
ViewConfigorNone