自動エスケープ機能をサポートするために、webapp アプリケーションで使用する jinja2 をインストールすることにしました。そこで、jinja2 を python 2.5 にインストールし、プロジェクト内にそのディレクトリを指すシンボリック リンクを作成しました。おおむね正常に動作しています。
例外として、実際に {% autoescape true %} タグを使用しようとすると、次のメッセージが表示されます。
File "/Users/me/project/templates/_base.html", line 1, in template
{% autoescape true %}
TemplateSyntaxError: Encountered unknown tag 'autoescape'.
ドキュメントにあるタグを使用しています:
{% autoescape true %} stuff {{var1}} stuff {{var2}}{% endautoescape %}
私のハンドラファイル内で、関連するものをインポートしています:
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from jinja2.ext import autoescape
エラーがスローされていないため、インポートは正常に機能しています。私は何か間違ったことをしていますか、それとも ext.py のように jinja2 自体に問題がありますか?
更新: 以下の sharth の提案を試してみましたが、同じ結果が得られました。彼の提案を使用して更新されたハンドラーを次に示します。
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)
繰り返しますが、autoescape タグを使用しない限り、問題なく動作します。