2014-02-24 44 views
1

因爲我需要在tpl文件中定義一個函數,所以我需要從瓶子切換。從bottle.template切換到mako

我的問題是雙重的:

  1. 一般情況下,是我現有的第三方物流仍然可用?
  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) 

但真子不知道我的 'device_view.tpl' 文件,我不確定'設備'是否作爲字典傳入。

回答

1

您可以定義函數在模板中使用,即使瓶子的SimpleTemplate

def func(): 
    ... 

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

{{func(data)}} 
+0

我不知道!謝謝。 – Jeff

0

我接受presveva的答案,但我想補充一點,我能在奶瓶使用的功能。我只需要將瓶子的舊版本更新到瓶子0.12。

我把它叫做是這樣的:

%variable_name = py_package_name.python_file.function_name(input_variable)