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:
  • app (Wijjit) – Application reference

  • views (dict[str, ViewConfig]) – Registered views

  • current_view (str or None) – Name of current view

  • default_view (str or None) – Name of default view

__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 view

  • default (bool) – Whether this is the default view (default: False)

  • on_enter (callable, optional) – Hook called when navigating to this view. Takes precedence over an on_enter key in a legacy dict return.

  • on_exit (callable, optional) – Hook called when navigating away from this view. Takes precedence over an on_exit key 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 + data callable 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(view_name, params=None)[source]

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:
  • view_name (str) – Name of view to navigate to

  • params (dict, optional) – Parameters to pass to the view’s data function

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:

ViewConfig or None