Skip to content

Defer rendering collected assets doesn't integrate with existing jinja2 environment #129

@ikus060

Description

@ikus060

When integrating Jinjax with an existing Jinja2 Environment (per the “Reusing an existing Jinja environment” guide), the new “defer rendering of collected assets” feature is not applied. This integration path renders templates via template.render(), which bypasses catalog.render() and therefore skips the asset finalization step.

Steps to Reproduce

  1. Set up Jinjax with an existing jinja2.Environment
  2. Render a template via template.render(context).
  3. Use components that enqueue assets expecting deferred rendering/finalization.

Expected

Deferred assets are collected and injected/finalized in the rendered HTML, as when using catalog.render().

Actual

Asset finalization doesn’t happen; the rendered HTML include a place holder "@@jinjax_assets_....@@".

Workaround

Call the private finalization helper directly on the produced HTML before returning it:

# after calling template.render(...)
html = template.render(context)

# Workaround: finalize assets manually
# NOTE: This is a private API and may change without notice.
html = catalog._finalize_assets(html)

return html

Notes

The underscore suggests `_finalize_assets` is private, so relying on it feels brittle.
There doesn’t seem to be a public way to finalize assets when using an external Jinja2 environment.

Suggestions

  • Documentation: Explicitly mention that when reusing an existing Jinja2 Environment, defer rendering requires a manual finalization step because template.render() bypasses catalog.render(). Provide an example using a supported/public API (see next point).
  • API: Expose a public method to finalize assets, e.g. catalog.finalize_assets(html)
  • Guide Update: In “Reusing an existing Jinja environment,” add a section “Defer rendering of collected assets” with a recommended pattern, for example:
template = env.get_template("page.html")
html = template.render(context)

# Recommended public API (hypothetical)
html = catalog.finalize_assets(html)

return html

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions