I decided to install jinja2 to use with my webapp application in order to support the autoescape functionality. So I installed jinja2 into python 2.5 and created a symlink within my project to point to that directory. It's mostly working fine.
EXCEPT, when I actually try to use the {% autoescape true %} tag, I get the message:
File "/Users/me/project/templates/_base.html", line 1, in template
{% autoescape true %}
TemplateSyntaxError: Encountered unknown tag 'autoescape'.
I'm using the tags as they have it in the docs:
{% autoescape true %} stuff {{var1}} stuff {{var2}}{% endautoescape %}
Within my handler file I'm importing the relevant stuff:
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from jinja2.ext import autoescape
And the import is working fine because it's not throwing an error. Am I doing something wrong, or is there a problem with jinja2 itself, like maybe in ext.py?
UPDATE: I tried sharth's suggestion below and got the same result. Here is my updated handler using his suggestion.
class MainHandler(BaseHandler):
def get(self):
self.context['testEscape']='<script type="javascript">alert("hi");</script>'
env = Environment(loader=FileSystemLoader([os.path.join(os.path.dirname(__file__), 'templates')]), autoescape=False)
template = env.get_template('index.html')
content = template.render(self.context)
self.response.out.write(content)
Again, it works fine as long as I don't use the autoescape tag.
The {% autoescape %}
tag needs Jinja 2.4 or higher and the jinja2.ext.autoescape
extension loaded.
env = Environment(autoescape=True, extensions=['jinja2.ext.autoescape'],
loader=...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With