【问题标题】:Best way to recursively include python Tornado templates递归包含 python Tornado 模板的最佳方法
【发布时间】:2025-11-28 21:40:02
【问题描述】:

我在我的 python 项目中,使用了 tornado 库:

1) 一个主要的 html 模板(网页结构),例如:

base.html

 <html>
 <head>....</head>
 <style>...</style>

 <div id="content">
 {{ sub_template_content }}
 </div>
</html>

2) 多个子模板,每个页面一个,如:

page1.html

<b>page 1 {{ content }}</b>

page2.html

<b>page 2 {{ content }}</b>

这个想法是始终使用“基本”模板构建页面,然后动态包含“子模板”(基于 get 参数)。

那么,最好的方法是什么?我已经尝试过使用龙卷风“包含”功能,但没有成功。谢谢!

【问题讨论】:

    标签: python html templates tornado


    【解决方案1】:

    请改用extends/block 功能。在 base.html 中:

    <div id="content">
        {% block content %}{% end %}
    </div>
    

    在page1.html中:

    {% extends "base.html" %}
    {% block content %}
        page 1 {{ content }}
    {% end %}
    

    【讨论】:

    • 谢谢!这正是我想要的!