If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

Features:

  • links in every breadcrumb
  • can set a breadcrumb to args
  • can localize/personalize the controllers - using T()

Thanks to Richard <richardbp(at)gmail.com>, for original function that I made improvements

Put in your model/module:

    def breadcrumbs(arg_title=None):
       "Create breadcrumb links for current request"
       # make links pretty by capitalizing and using 'home' instead of 'default'
       #pretty = lambda s: s.replace('default', 'Início').replace('_', ' ').capitalize()
       menus = [A(T('Home'), _href=URL(r=request, c='default', f='index'))]
       if request.controller != 'default':
           # add link to current controller
           menus.append(A(T(pretty(request.controller)), _href=URL(r=request, c=request.controller, f='index')))
           if request.function == 'index':
               # are at root of controller
               menus[-1] = A(T(pretty(request.controller)), _href=URL(r=request, c=request.controller, f=request.function))
           else:
               # are at function within controller
               menus.append(A(T(pretty(request.function)), _href=URL(r=request, c=request.controller, f=request.function)))
           # you can set a title putting using breadcrumbs('My Detail Title') 
           if request.args and arg_title:
               menus.append(A(T(arg_title)), _href=URL(r=request, c=request.controller, f=request.function,args=[request.args]))
       else:
           #menus.append(A(pretty(request.controller), _href=URL(r=request, c=request.controller, f='index')))
           if request.function == 'index':
               # are at root of controller
               #menus[-1] = pretty(request.controller)
               pass
               #menus.append(A(pretty(request.controller), _href=URL(r=request, c=request.controller, f=request.function)))
           else:
               # are at function within controller
               menus.append(A(T(pretty(request.function)), _href=URL(r=request, c=request.controller, f=request.function)))
           # you can set a title putting using breadcrumbs('My Detail Title') 
           if request.args and arg_title:
               menus.append(A(T(arg_title), _href=URL(r=request, f=request.function,args=[request.args])))

       return XML(' > '.join(str(m) for m in menus))

To use, put in your view:

 {{=breadcrumbs() }}

or

  {{=breadcrumbs('My Title for request.args ') }}

To use in your layout.html, you can use session

 {{=breadcrumbs(session.titleargs) }}

and set in your model

 session.titleargs = None

and in your function:

 session.titleargs = 'my title for args'

Related slices

Comments (3)

  • Login to post



  • 0
    mdmcginn 14 years ago
    Very cool! Just a little change in the return statement will give you access to the breadcrumbs in css so you can style them. Example: return XML('')

  • 0
    mdmcginn 14 years ago
    not sure why but the code snippet I submitted was altered! Maybe a triple quote will help? """return XML('')""" The div should have a class attribute called breadcrumbs. And the greater than sign should be escaped...

  • 0
    mdmcginn 14 years ago
    Ok I read the markdown page... One more try! `return XML('') `

Hosting graciously provided by:
Python Anywhere