繁体   English   中英

Tornado 模板调用包括使用变量

[英]Tornado template call include using variable

我正在尝试使用龙卷风模板动态包含 html 页面

{% set tab = (handler.request.arguments).get("tab",[b"input"])[0].decode('utf-8') %}
{% set page_path =  "{}.html".format(tab) %}
{% include page_path %}

但不知何故这会引发错误:

FileNotFoundError: [Errno 2] No such file or directory: '/home/usr/Data/app/static/html/page_path'

我也试过使用

{% include {{page_path}} %}

这也会引发错误:

FileNotFoundError: [Errno 2] No such file or directory: '/home/usr/Data/app/static/html/{{page_path}}'

有没有办法在龙卷风模板中动态使用变量?

根据 Tornado 文档,可以使用这个:

``{% module *expr* %}``
    Renders a `~tornado.web.UIModule`.  The output of the ``UIModule`` is
    not escaped::

        {% module Template("foo.html", arg=42) %}

    ``UIModules`` are a feature of the `tornado.web.RequestHandler`
    class (and specifically its ``render`` method) and will not work
    when the template system is used on its own in other contexts.

因此,使用此代码段将支持使用变量的龙卷风模板:

{% set tab = (handler.request.arguments).get("tab",[b"input"])[0].decode('utf-8') %}
{% set page_path =  "{}.html".format(tab) %}
{% module Template(page_path) %}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM