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

This version mantains all funcionality of easy breadcrumbs, but allows more options, to have a completily personalized breadcrumb.

In slice, I use the example of a user making a order for a client.

But first,, you will put the function in a model or module:

    def breadcrumb(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('Início', _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:
                try:
                    for parent in session.bc[request.function]['parents']: 
                        url = session.bc[request.function]['parents'][parent]['url']
                        title = session.bc[request.function]['parents'][parent]['title']
                        menus.append(A(T(title),  _href=url))

                except :
                     pass
                # except HTTP, e:
                     #print 'error', e, 'occurred'

                finally:
                    try:
                        if not session.blockfunction:
                            menus.append(A(T(pretty(request.function)), 
                                   _href=URL(r=request, c=request.controller, f=request.function)))
                    except:
                        pass
            if request.args and arg_title:
                args=request.args
                menus.append(A(T(arg_title), _href=URL(r=request,f=request.function,args=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
                # 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')
                try:
                    for parent in session.bc[request.function]['parents']: 
                        url = session.bc[request.function]['parents'][parent]['url']
                        title = session.bc[request.function]['parents'][parent]['title']
                        menus.append(A(T(title),  _href=url))

                except :
                     pass
                # except HTTP, e:
                     #print 'error', e, 'occurred'

                finally:
                    try:
                        if not session.blockfunction:
                            menus.append(A(T(pretty(request.function)), 
                                   _href=URL(r=request, c=request.controller, f=request.function)))
                    except:
                        pass

            if request.args and arg_title:
                args=request.args
                menus.append(A(T(arg_title), _href=URL(r=request, f=request.function,args=args)))

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

And add in your view or layout.html

        {{if session.arg_title:}}
            {{=breadcrumb(session.arg_title)}}

        {{else:}}   
            {{=breadcrumb()}}
        {{pass}}

The user go first to 'clients' page/function, a list of clients

/default/clients

To the clients function, the slice generate automatically:

Home > Clients

The slice automatically adds a breadcrumb to controller when it is not 'default' :

Home > My Controller > Clients

to

/my_controller/clients

So, to make it simple, I will only use samples of 'default' controller

After, the user choose a client e the slice generates automatically:

Home > Client

to

/default/client/13

But you want

Home > Clients > Client

So you need to set the parent function, them put in you function:

       def client(): # your function 
        # 'order' is the name of function 
        # 0 is the number of parent function, it will be show after 'Home'
        # title is the name of parent function
        # url is the link to parent function 
        # in the sample, clients is a list of clients
        session.bc = {'client':
                {'parents':
                {0:
                {'title': 'Clients',
                 'url':URL(r=request, f='clients')}}}}
        ...your code....
        return ....

But now will want

Home > Clients > Pedro Goes

where Pedro Goes is the name of the client

add

 session.blockfunction = True
 session.arg_title= db.client[request.args(0)].name

Now, the user will do a order to the client

/default/order/

Its generates automatically

Home > Order

But you want the breadcrumb:

Home > Clients > Pedro Goes > Order

Where Pedro Goes is the name of your client

So you can set in your 'client' function (or whatever your parent function is) :

  session.client_name = client.name 
  session.client_id = client.id

Then To set more than one parent function:

       def order(): # your function 
        # 'order' is the name of function 
        # 0 is the number of parent function, it will be show after 'Home'
        # title is the name of parent function
        # url is the link to parent function 
        session.bc = {'order_detail':
            {'parents':{0:
                         {'title': '',
                         'url':URL(r=request, f='clients')},
                        1:
                         {'title': session.client_name,
                          'url':URL(r=request, f='client',args=[session.client_id]) }                              
                        }
                        }}
        ...your code....
        return ....

Then the user can go to view the order:

/default/order/34

where 34 is the request.args(0) and will be used as order.id

It still mantain:

Home > Clients > Pedro Goes > Order

But you want

Home > Clients > Pedro Goes > Order nº: 34

       def order(): # your function 
        # 'order' is the name of function 
        # 0 is the number of parent function, it will be show after 'Home'
        # title is the name of parent function
        # url is the link to parent function 
        session.arg_title= 'Order nº : ' + order.id 
        session.blockfunction = True
        session.bc = {'order_detail':
            {'parents':{0:
                         {'title': '',
                         'url':URL(r=request, f='clients')},
                        1:
                         {'title': session.client_name,
                          'url':URL(r=request, f='client',args=[session.client_id]) }                              
                        }
                        }}
        ...your code....
        return ....

Related slices

Comments (0)


Hosting graciously provided by:
Python Anywhere