Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 toSafeString
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 theTEMPLATES["OPTIONS"]["globals"]
setting. But for other usages you can do the same thing by updating theenv.globals
attr of yourjinja2.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.