pythoncherrypygenshi

Context specific navigation in Genshi and CherryPy


Using the Genshi templating engine with CherryPy, I need to have a context-specific site navigation which displays a different menu for logged in users.

Users are identified by a CherryPy session.

What would be the best way to show a different menu for logged in users?


Solution

  • We check to see if the user is logged in...

    import cherrypy
    from genshi.template import TemplateLoader
    
    @cherrypy.expose
    def index(self):
    tmpl = loader.load('index.html')
    
    if(cherrypy.session.get('_cp_Email')):
        return tmpl.generate(title='Geddit').render('html', LoggedIn=True)
    else:
        return tmpl.generate(title='Geddit').render('html', LoggedIn=False)
    

    your template would like something like this...

                if (LoggedIn) {
                  # Logged In menu
                } else {
                  # not Logged In menu
                }
    

    If they are we send a variable to the template to show Log Out instead of Login.

    We're using this for Authentication...

    http://tools.cherrypy.org/wiki/AuthenticationAndAccessRestrictions

    Include the comments for security reasons. Hope this helps!