overrideable autoescape
See original GitHub issueAt work, we are using Jinja to render templates as YAML. We’d like to customize all {{ variable }}
expansions to do something sensible to Python values like None
(which is null
in YAML) and empty string (which is encoded as ''
in YAML). It seems like the Jinja autoescaping mechanism would be perfect for this. Unfortunately, I don’t see any way to configure the autoescaping functionality to not autoescape to HTML. Instead, I’ve had to monkeypatch Markup
and escape
in jinja2.runtime
. It would be nice if there were an officially-sanctioned method to do this, for example by overriding something in the environment.
Issue Analytics
- State:
- Created 8 years ago
- Comments:11 (3 by maintainers)
Top Results From Across the Web
API — Jinja Documentation (3.0.x)
In future versions of Jinja we might enable autoescaping by default for ... Subclasses may override this method and implement template path joining...
Read more >Escape Velocity Override - Wikipedia
Escape Velocity Override is a space trading simulator game written by Peter Cartwright, with the support of his school-friends, and developed by Ambrosia ......
Read more >Override escape functionality in jinja2 - Stack Overflow
So the question is: How can I override the jinja2.escape() function? I tried doing something like import jinja2.utils def latex_escape(test): ...
Read more >Escape Velocity Override - EVN Wiki - Fandom
Escape Velocity Override (abbreviated EV Override) is a Macintosh computer game by Peter Cartwright, published by Ambrosia Software.
Read more >Override Checking - Sorbet
Override Checking · A note on variance · What if I really want the child method to narrow the type? · Escape hatches...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Using Jinja templates or any other string-based template language or string operations at all to generate JSON is just wrong. I can see why you would do this for YAML since it is meant to be human-friendly, too, but JSON should be generated from e.g. a Python dict and not from a Jinja template.
(that said, I am curious why you want to do that and what you are trying to do :p)
The first part of my solution was the extension that runs all variable calls through a filter. This is the bare minimum that I needed to get that working.
This could be smarter, for example it could try to skip inserting the filter if it sees a
safe
oryaml
filter explicitly in the variable block. But I never ended up needing/having time for that.The second part is the filter itself. Just using
yaml.dump
wasn’t sophisticated enough, so I had to poke around the yaml internals a little bit.This ties it all together, including making sure that the filter of the given name is available in the environment: