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

support Jinja2 template backend #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

anentropic
Copy link

@anentropic anentropic commented May 31, 2024

I was hoping to use this in a Django+Jinja project.

Looking at this issue: #8 ...a solution is provided in the comments by @wizpig64

It works quite serendipitously because the tags in django-vite are all using @register.simple_tag decorator... and that ultimately returns the decorated function untouched (it just does registration as a custom tag).

The other decorator applied is @mark_safe, which promotes the return value to SafeString str subclass... which will have no effect for Jinja, but is harmless.

And that works ok currently because Jinja does not autoescape by default (otherwise because it's not 'marked safe' for Jinja the rendered HTML tags from django_vite would get escaped if you have autoescape turned on... the docs hint that may become default in future).

I think the only django-jinja specific part in the comment is the TEMPLATES["OPTIONS"]["globals"] setting. But for other usages you can do the same thing by updating the env.globals attr of your jinja2.Environment instance.

Given the caveats above I have prepared this PR to explicitly provide a Jinja extension that is safe for autoescaping, and still taking advantage of the fact the tags can share a common implementation.

@anentropic anentropic force-pushed the jinja branch 2 times, most recently from 2dc2871 to 2ff1d5f Compare May 31, 2024 22:49
@Niicck
Copy link
Collaborator

Niicck commented Jun 21, 2024

Thank you for this contribution! This feature looks good and I appreciate the docs and tests.

I'd have one request -- since @marksafe and @simple_tag don't have any impact on jinja, can we keep the original django_vite.py templatetags file in place and just import those straight into the jinja.py templatetags file? I'm not sure what the new base.py gets us, and I find it cleaner to have the decorators directly attached to the templatetag they belong to. I think this PR could get away with only needing extra wrappers around the jinja tags.

@KevinMind
Copy link

Is this PR stale? I have the same desire to use this lib with an existing jinja2 based project and wondering if this is something that could be picked up?

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.

3 participants