component_framework.adapters.jinjax_renderer

Jinjax renderer implementation.

 1"""Jinjax renderer implementation."""
 2
 3try:
 4    from jinjax import Catalog
 5except ImportError as e:
 6    from . import _require_extra
 7
 8    raise _require_extra("jinjax", "fastapi") from e
 9
10from ..core import Renderer
11
12
13class JinjaxRenderer(Renderer):
14    """Renderer using the JinjaX component system."""
15
16    def __init__(self, catalog: Catalog):
17        """
18        Initialize the JinjaX renderer.
19
20        Pass the ``Catalog`` your application already configured rather than a
21        fresh one. The renderer renders through this catalog's Jinja
22        environment, so it inherits every custom filter, global (e.g.
23        ``url_for``), and extension registered on it. A brand-new ``Catalog()``
24        has its own empty environment, so component templates would silently
25        lose that context. When sharing with ``Jinja2Templates``, build the
26        catalog from the same env, e.g. ``Catalog(jinja_env=templates.env)``.
27
28        Args:
29            catalog: The JinjaX ``Catalog`` instance to render through —
30                ideally the one shared with the rest of your app.
31        """
32        self.catalog = catalog
33
34    def render(self, template_name: str, context: dict) -> str:
35        """Render Jinjax component."""
36        return self.catalog.render(template_name, **context)
class JinjaxRenderer(component_framework.core.renderer.Renderer):
14class JinjaxRenderer(Renderer):
15    """Renderer using the JinjaX component system."""
16
17    def __init__(self, catalog: Catalog):
18        """
19        Initialize the JinjaX renderer.
20
21        Pass the ``Catalog`` your application already configured rather than a
22        fresh one. The renderer renders through this catalog's Jinja
23        environment, so it inherits every custom filter, global (e.g.
24        ``url_for``), and extension registered on it. A brand-new ``Catalog()``
25        has its own empty environment, so component templates would silently
26        lose that context. When sharing with ``Jinja2Templates``, build the
27        catalog from the same env, e.g. ``Catalog(jinja_env=templates.env)``.
28
29        Args:
30            catalog: The JinjaX ``Catalog`` instance to render through —
31                ideally the one shared with the rest of your app.
32        """
33        self.catalog = catalog
34
35    def render(self, template_name: str, context: dict) -> str:
36        """Render Jinjax component."""
37        return self.catalog.render(template_name, **context)

Renderer using the JinjaX component system.

JinjaxRenderer(catalog: jinjax.catalog.Catalog)
17    def __init__(self, catalog: Catalog):
18        """
19        Initialize the JinjaX renderer.
20
21        Pass the ``Catalog`` your application already configured rather than a
22        fresh one. The renderer renders through this catalog's Jinja
23        environment, so it inherits every custom filter, global (e.g.
24        ``url_for``), and extension registered on it. A brand-new ``Catalog()``
25        has its own empty environment, so component templates would silently
26        lose that context. When sharing with ``Jinja2Templates``, build the
27        catalog from the same env, e.g. ``Catalog(jinja_env=templates.env)``.
28
29        Args:
30            catalog: The JinjaX ``Catalog`` instance to render through —
31                ideally the one shared with the rest of your app.
32        """
33        self.catalog = catalog

Initialize the JinjaX renderer.

Pass the Catalog your application already configured rather than a fresh one. The renderer renders through this catalog's Jinja environment, so it inherits every custom filter, global (e.g. url_for), and extension registered on it. A brand-new Catalog() has its own empty environment, so component templates would silently lose that context. When sharing with Jinja2Templates, build the catalog from the same env, e.g. Catalog(jinja_env=templates.env).

Arguments:
  • catalog: The JinjaX Catalog instance to render through — ideally the one shared with the rest of your app.
catalog
def render(self, template_name: str, context: dict) -> str:
35    def render(self, template_name: str, context: dict) -> str:
36        """Render Jinjax component."""
37        return self.catalog.render(template_name, **context)

Render Jinjax component.