Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-dict return value support for jinja #11

Merged
merged 3 commits into from
Feb 16, 2024

Conversation

volfpeter
Copy link
Owner

@volfpeter volfpeter commented Feb 16, 2024

Changes:

  • Jinja now supports non-dict route return values by default. The conversion from such values to valid Jinja contexts (dict) is done by JinjaContextFactory methods with default implementations in JinjaContext.
  • It's possible to set the default Jinja context factory through the Jinja.make_context property.
  • It's possible to override the Jinja instance's context factory on a per-route basis using the make_context argument of the decorator.
  • Jinja got a new hook method _make_response() for response customization.
  • Updated the documentation to highlight these features.

@volfpeter volfpeter merged commit f9ffbe7 into main Feb 16, 2024
2 checks passed
@volfpeter volfpeter deleted the non-dict-return-value-support-for-jinja branch February 16, 2024 21:19
@shawnsw
Copy link

shawnsw commented Feb 17, 2024

@volfpeter Great work. Small suggestion: Instead of using class names like Jinja that may cause confusion, can we call it something like HxTempl to differentiate? It won't matter much to the code, but makes the discussions easier. Also can you please give a quick example of how to set response cookies with this new update?

@volfpeter
Copy link
Owner Author

Small suggestion: Instead of using class names like Jinja that may cause confusion, can we call it something like HxTempl to differentiate?

The short answer is no. This lib is not a Jinja-specific one, so calling the Jinja-related classes anything else would be confusing. This would also be a breaking change, which I'd avoid as long as possible. If you prefer different names, you can alias these names in your imports.

Also can you please give a quick example of how to set response cookies with this new update?

I don't recommend doing it, but here is pattern:

def get_cookie_updates() -> dict:
    # FastAPI dependency that returns an object that describes the changes you'd like to make to the response cookies
    ...

class JinjaWithCookieUpdate(Jinja):
    def _make_response(
        self,
        template_name: str,
        *,
        jinja_context: dict[str, Any],
        request: Request,
    ) -> HTMLResponse:
        result = super._make_response(template_name, jinja_context=jinja_context, request: Request)
        if "cookie_updates" in jinja_context:
            # TODO: make the cookie changes on result
            ...
        return result

jinja = JinjaWithCookieUpdate(
    templates=Jinja2Templates("templates"),
    make_context=JinjaContext.unpack_result_with_route_context
)

@app.post("/something")
def something(cookie_updates: dict = Depends(get_cookie_update)):
    return None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants