Add a Breadcrumb Subscriber to a Pyramid project using 4 simple steps

This article will explain how to add a breadcrumb subscriber to a Pyramid project using 4 simple steps.

While programming I needed the ability to easily create breadcrumb links from the current url. You may view the source code here. The following guide describes the process I took to add this functionality.

1. Download and include at the top of your Pyramid project’s file:
    from yourproject.bread import Bread
    from import subscriber, NewRequest
2. At the bottom of the projects file create the following subscriber function as follows:
    def bread_subscriber( event ):
        """ Build Bread object and add to request """
        event.request.bread = Bread( event.request.url )
3. Now we can add our new subscriber to our Pyramid config inside main and above the routes:
    config.add_subscriber( base_subscriber, NewRequest )
4. You are done! Now you should have the ability to use the bread object from your template. Below I have provided a mako template snippet:
    % for link in request.bread.links:
      ${ link | n } /
    % endfor

6 thoughts on “Add a Breadcrumb Subscriber to a Pyramid project using 4 simple steps

  1. Wow, that was dead simple man! Thanks for the little tutorial!

    I was thinking of integrating your solution with Twitter Bootstrap’s Bread crumb ui.

  2. @update :

    I solved my error by tweaking a bit your links function :

    def links( self ):
    links = []
    for count, crumb in enumerate( self.crumbs, start = 1 ):
    crumb_uri = self._protocol + '/'.join( self.crumbs[ 0:count ] )
    links.append({'uri':crumb_uri, 'crumb':crumb})
    return links

    I also embedded it in a Boostrap breadcrumb (as suggested earlier) :

    % for link in request.bread.links:

    ${link['crumb']} /

    % endfor

    Thanks again for this useful tool !


  3. The problem wasn’t with the links method, the problem was with my example mako template code:

    % for link in links:
    ${link|n} /
    % endfor

    Newer versions of pyramid escape HTML. To prevent escaping HTML (to send HTML into your template from your view) use | n

    I updated the example above to be more correct.

Leave a Reply

Your email address will not be published. Required fields are marked *