繁体   English   中英

从bottle.template切换到mako

[英]Switching from bottle.template to mako

因为我需要在tpl文件中定义一个函数,所以需要从bottle切换。

我的问题有两个:

  1. 总的来说,我现有的tpls是否仍然可用?
  2. 如何更改server.py中的退货?

这是现有的代码:

@bottle.route('/showDevice')
def device_view():
    device_id = bottle.request.query.id
    result = lib.crud_ops.find_by_id(collection, device_id)
    return bottle.template('device_view.tpl', {'device':result})

我尝试添加一些内容:

 myTemplate = Template(filename='device_view.tpl')
 myTemplate.render(device=result)

但是Mako不知道我的“ device_view.tpl”文件在哪里,而且我不确定“设备”是否作为字典传递。

您甚至可以使用bottle的SimpleTemplate定义要在模板中使用的函数:

def func():
  ...

def device_view():
  return bottle.template('device_view.tpl', {'device':result, 'func':func})

{{func(data)}}

我接受了presveva的回答,但我想补充一点,就是我能够使用bottle中的函数。 我只需要从旧版本的bottle-0.10更新到bottle-0.12。

我这样称呼它:

%variable_name = py_package_name.python_file.function_name(input_variable)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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