在Bottle中使用SimpleTemplate

时间:2013-11-22 13:57:26

标签: template-engine bottle

我是像Frame这样的新工具,并且正在完成文档/教程。 现在我遇到了模板引擎的问题:

我的文件夹index.tpl中有一个名为views的文件。 (这是简单的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()

但如果我这样做,浏览器会向我显示一个带有

字样的白页
  

索引

编写,而不是加载模板。
在文档中,没有关于我如何使用这种面向对象方法的进一步信息。 我无法弄清楚为什么会发生这种情况以及我必须如何做到这一点......

2 个答案:

答案 0 :(得分:4)

根据您原始问题的精神,这是一个很好,简单的解决方案:

tpl = SimpleTemplate(name='views/index.tpl')  # note the change here

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

答案 1 :(得分:-2)

关于SimpleTemplate:

>>> from bottle import SimpleTemplate
>>> tpl = SimpleTemplate('Hello {{name}}!')
>>> tpl.render(name='World')
u'Hello World!'