Random   •   Archives   •   RSS   •   About   •   Contact

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

Note: This post shows an old way to modify the request object. I discuss a new way in my Pyramid add_request_method post.

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

While programming https://school.yohdah.com/ I needed the ability to easily create breadcrumb links from the current url. You may view the bread.py source code here. The following guide describes the process I took to add this functionality.

1. Download and include bread.py at the top of your Pyramid project's __init__.py file:

from yourproject.bread import Bread
from pyramid.events import subscriber, NewRequest

2. At the bottom of the projects __init__.py 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(bread_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

Want comments on your site?

Remarkbox — is a free SaaS comment service which embeds into your pages to keep the conversation in the same place as your contentr. It works everywhere, even static HTML sites like this one!

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

© Russell Ballestrini.