【问题标题】:Using SimpleTemplate in Bottle在 Bottle 中使用 SimpleTemplate
【发布时间】:2013-11-22 13:57:26
【问题描述】:

我是 Bottle 等框架的新手,正在阅读文档/教程。 现在我遇到了模板引擎的问题:

我的文件夹views 中有一个名为index.tpl 的文件。 (这是纯 html)
当我使用以下代码时,它可以显示我的 html:

from bottle import Bottle, SimpleTemplate, run, template

app = Bottle()

@app.get('/')
def index():
    return template('index')

run(app, debug=True)

现在我想在我的项目中实现这个引擎,不想使用template()
我想按照文档中的方式使用它,例如:

tpl = SimpleTemplate('index')

@app.get('/')
def index():
    return tpl.render()

但如果我这样做了,浏览器只会显示一个带有单词的白页

索引

编写,而不是加载模板。
在文档中,没有关于我如何使用这种 OO 方法的更多信息。 我只是想不通为什么会发生这种情况以及我必须如何正确地做到这一点......

【问题讨论】:

    标签: template-engine bottle


    【解决方案1】:

    本着您最初问题的精神,这是一个不错的简单解决方案:

    tpl = SimpleTemplate(name='views/index.tpl')  # note the change here
    
    @app.get('/')
    def index():
        return tpl.render()
    

    【讨论】:

    • 非常感谢,这看起来很明显。 :D
    • 乐于助人。 (事后诸葛亮让一切看起来很明显。:) 如果答案解决了您的问题,请考虑将其标记为接受。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-10-15
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多