简体   繁体   中英

Tornado template call include using variable

I am trying to dynamically include html pages using tornado template

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

But somehow this throws error:

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

I have also tried using

{% include {{page_path}} %}

Which also throws the error:

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

Is there any way to dynamically use variables in tornado templates?

According to Tornado Documentation one can use this:

``{% 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.

So using this snippet will support tornado template using variables:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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