Hello,
I am building a JBar menubar widget for a TG application. I was planning to build different menubars by subclassing a base JBar(Widget) class and modifying the template. The problem is that I need to use tg.url and request objects to modify the menu from the controller. When I call the display() method on my
HomeMenuBar, I get:
UndefinedError: "request" not definedSo, I'm not getting access to request (or tg.url) in the environment of my widget template. How do I do this?
To summarize what is happening (ToscaWidgets 0.9.12, TurboGears2 2.2.0):
class HomeMenuBar (JBar): template=(''' <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/" xmlns:xi="http://www.w3.org/2001/XInclude" py:attrs="attrs" py:strip=""> <ul class="${menu_css}"> <li><a py:if="request.identity" href="${tg.url('/about')}">About</a></li> <li><a py:if="request.identity" href='/pdf/User Manual.pdf'>Help</a></li> <li><a py:if="request.identity" id="login" class="loginlogout" href="${tg.url('/logout_handler')}">Logout</a></li> </ul> </html> ''')home_menu_bar=HomeMenuBar('home_menu') I create the object once in my
widgets.MenuBar.py and pass from the controller to another template in the tmpl_context:
@expose('MyApp.templates.TableView')
@expose('json')
def home (self, *args, **kw):
...
tmpl_context.menu_bar=MenuBars.home_menu_bar
return {}
In my
TableView.html template, I include:
<xi:include href="master.html" />
which finally brings in the menu bar across the top of each page:
....
<div py:if="hasattr(tmpl_context, 'menu_bar')" id="menu_bar_container">
${tmpl_context.menu_bar.display()}
</div>
....
I had previously passed a genshi method name from controller to template in order to accomplish the same task, but I wanted to put the menubar into a widget, as this allows more flexibility to modify the menu in the controller.
Thank you,
Shane