【问题标题】:How to enable bottle to search multiple paths for rendering templates?如何让瓶子搜索渲染模板的多个路径?
【发布时间】:2018-10-21 04:05:56
【问题描述】:

我正在尝试找到一种方法来搜索渲染模板的多个路径,而不仅仅是/views 目录。我想将我的基本模板和页面模板与我的包含(例如我的头部、页眉和页脚)分开。我想在我的/views 目录中嵌套一个/includes 目录,该目录在渲染模板时会进行瓶子搜索。我尝试使用TEMPLATE_PATHS,但我无法让它工作。有人能指出我正确的方向吗,谢谢。

【问题讨论】:

    标签: python html templates bottle


    【解决方案1】:

    修改bottle.TEMPLATE_PATH 列表变量,将其附加到您希望Bottle 查找模板的任何其他路径。请参阅docs

    例如:

    from bottle import route, run, template, TEMPLATE_PATH
    
    TEMPLATE_PATH.append('./other_templates')
    
    
    @route('/hello')
    @route('/hello/<name>')
    def hello(name='World'):
        return template('hello_template', name=name)
    
    run(host='localhost', port=8080)
    

    我的文件结构如下:

    .
    ├── other_templates
    │   └── hello_template.tpl
    └── server.py
    

    【讨论】:

    • 谢谢,这正是我想要的。
    • @zack-stone 很高兴它有帮助!那么我们可以将其标记为正确答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2016-10-31
    • 2013-06-16
    • 2014-07-17
    • 2018-03-25
    • 1970-01-01
    • 2020-10-21
    相关资源
    最近更新 更多