Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem with jinja2 autoescape in google app engine webapp

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.

like image 526
Bob Ralian Avatar asked Jan 12 '11 21:01

Bob Ralian


1 Answers

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=...)
like image 161
Armin Ronacher Avatar answered Nov 15 '22 01:11

Armin Ronacher