Jinja ¶
Jinja is a "general purpose templating language."
Weaknesses¶
Warning:
Jinja's escaping will not protect you from Cross-Site Scripting if you do not quote your HTML attributes.
Always quote your HTML attributes!
Escaping¶
Jinja has manual HTML entity escaping through the |e filter or automatic HTML entity escaping through the autoescaping extension.
The escape method is shown below. It replaces all occurrences of &, <, >, ', and " characters in a variable. Characters are replaced in that order with their respective HTML entity encoded counterparts.
791 return Markup(unicode(s)
792 .replace('&', '&')
793 .replace('>', '>')
794 .replace('<', '<')
795 .replace("'", ''')
796 .replace('"', '"')
797 )
Automatic Escaping¶
In Jinja 2.4 there is autoescaping functionality which will escape any variable displayed in a template. The autoescape extension must be enabled in the environment. To enable the autoescape extension, modify your Environment instantiation to look similar to this:
env = Environment(autoescape=True, extensions=['jinja2.ext.autoescape'])
Manual Escaping¶
There is a |e filter which manually HTML escapes the variable passed through it.
-
Wiki content is available under a Creative Commons 3.0 License.
