简体   繁体   中英

Switching from bottle.template to mako

Because I need to define a function in a tpl file, I need to switch from bottle.

My question is two-fold:

  1. In general, are my existing tpls still usable?
  2. How do I change my return in my server.py?

Here is the existing code:

@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})

I have tried adding a few things:

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

but Mako doesn't know where my 'device_view.tpl' file is, and I'm not sure if 'device' is being passed in as a dictionary.

You can define functions to be used in the template even with bottle's SimpleTemplate :

def func():
  ...

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

{{func(data)}}

I accepted presveva's answer, but I wanted to add that I was able to use functions in bottle. I just had to update from the old version of bottle-0.10 to bottle-0.12.

I called it like this:

%variable_name = py_package_name.python_file.function_name(input_variable)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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