Search code examples
pythonmongodbpymongobottlemako

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.


Solution

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