When a method is exposed, it can return a dict used by a template :
class RootController(TGController):
@expose('myapp.templates.index')
def index(self):
self.mykey = "foo"
self.mymenu = ["item1", "item2", "item3"]
self.selected = "item1"
return dict( mykey=self.mykey, mymenu=self.mymenu, selected=self.selected)
This code works fine. Now I would like to encapsulate the menu boiler plate into a decorator like this :
class RootController(TGController):
@expose('myapp.templates.index')
@menu()
def index(self):
self.mykey = "foo"
self.mymenu = ["item1", "item2", "item3"]
self.selected = "item1"
return dict( mykey=self.mykey)
But I don't know how to write this menu decorator. If I use :
def before_render_cb(remainder, params, output):
return output.update( dict(mymenu=["item1", "item2", "item3"], selected="item1"))
class RootController(TGController):
@expose('myapp.templates.index')
@before_render(before_render_cb)
def index(self):
self.mykey = "foo"
self.mymenu = ["item1", "item2", "item3"]
self.selected = "item1"
return dict( mykey=self.mykey)
It will add mymenu and selected into the dict but I don't have access to the instance attribute of the controller (self.mymenu and self.selected)
If I use a decorator :
class menus(object):
def __call__(self, func):
deco = Decoration.get_decoration(func)
return func
I can have access to the decoration but not to the expose object neither to the controller.
How can I do this?
Here is my example for understanding "before_render" decorator:
As You can see, "before_render" decorator intercepts sent data, works on it and returns another data. "output" is a local variable for "before_render". We changed and updated it's consistence, then sent it as new data.