tornado模板的自动编码问题(autoescape )

tornado新版(具体版本未知)支持自动转义,比如{{ "<div>" }}不会输出html标签,而是进行转义。

{{ module.test(10) }} 也会进行转义。往往module的调用是输出html的,转义之后就没法使用了。

解决方法有2种:

1. 通过{% autoescape None %}关掉自动转义,但是此命令的作用于是整个文件,要么全自动转义,要么全都不转义

2. 使用{% module %}命令输出原始html.

``{% module *expr* %}``

Renders a `~tornado.web.UIModule`.  The output of the ``UIModule`` isnot escaped::{% module Template("foo.html", arg=42) %}

使用场景:

如果一个文件中多数需要转义,则打开{% autoescape xhtml_escape %},遇到不需要转义的则通过{% module *expr* %}处理

如果一个文件多数不需要转义,则{% autoescape None %},遇到需要转义的使用{{ escape(xxx) }}处理

update:

{% module *expr* %} 这种方法可以直接调用module类,返回不会被转义。

如果直接输出html,可以使用raw的方式,比如 {% raw "<div>xxx</div>""" %},对<div>不会进行转义处理

原文地址:https://www.cnblogs.com/jsben/p/5296498.html