Project description
The z3c.breadcrumb package provides base classes for breadcrumb
implementations. It allows you to write adapters for each content object which
provides it’s own rules for providing the breadcrumb name, url and selection.
Detailed Documentation
README
The z3c.breadcrumb package provides base classes for breadcrumb
implementations. It allows you to write adapters for each content object which
provides it’s own rule for providing the breadcrumb name, url and selection.
Let’s do some imports we will use later.
>>> import zope.interface
>>> import zope.component
>>> from zope.publisher.interfaces.http import IHTTPRequest
>>> from zope.publisher.browser import TestRequest
>>> from zope.traversing.browser import absoluteURL
>>> from zope.container import contained
>>> from z3c.breadcrumb import interfaces
>>> from z3c.breadcrumb import browser
IBreadcrumb
Let’s define a interface and a content object.
>>> class IOffice(zope.interface.Interface):
... """Office interface."""
>>> @zope.interface.implementer(IOffice)
... class Office(contained.Contained):
... def __init__(self, label):
... self.label = label
... self.activeURL = True
>>> office = Office('Zope Foundation')
>>> office.__name__ = 'ZF'
There is a generic breadcrumb implementation which is registered by
default. If we do not implement a custom IBreadcrumb the generic adapter will
return the title or __name__ of the item. Let’s register the default
adapter, this is normally done in configure.zcml :
>>> zope.component.provideAdapter(browser.GenericBreadcrumb)
And see what we get:
>>> request = TestRequest()
>>> breadcrumb = zope.component.getMultiAdapter((office, request),
... interfaces.IBreadcrumb)
>>> breadcrumb.name
'ZF'
We can also implement a custom IBreadcrumb adapter and provide another
name for the breadcrumb name:
>>> @zope.interface.implementer(interfaces.IBreadcrumb)
... @zope.component.adapter(IOffice, IHTTPRequest)
... class BreadcrumbForOffice(object):
...
... def __init__(self, context, request):
... self.context = context
... self.request = request
...
... @property
... def name(self):
... return self.context.label
...
... @property
... def url(self):
... return absoluteURL(self.context, self.request)
...
... @property
... def activeURL(self):
... return self.context.activeURL
Let’s register the custom IBreadcrumb adapter for IOffice:
>>> zope.component.provideAdapter(BreadcrumbForOffice)
And check the new breadcrumb name:
>>> breadcrumb = zope.component.getMultiAdapter((office, request),
... interfaces.IBreadcrumb)
>>> breadcrumb.name
'Zope Foundation'
CustomNameBreadcrumb
Let’s define another interface and a content object.
>>> class IOfficeContainer(zope.interface.Interface):
... """Container of offices."""
>>> @zope.interface.implementer(IOfficeContainer)
... class OfficeContainer(contained.Contained):
... pass
>>> offices = OfficeContainer()
>>> offices.__name__ = 'offices'
If the custom name for this kind of object is always the same it would quickly
get tedious to write a full IBreadcrumb implementation. As a shortcut you
can use CustomNameBreadcrumb to get an adapter that acts like
GenericBreadcrumb, but returns the name you want.
>>> adapter = browser.CustomNameBreadcrumb('Offices')
>>> adapter
<class 'z3c.breadcrumb.browser.CustomNameBreadcrumb('Offices')'>
>>> zope.component.provideAdapter(adapter,
... adapts=(IOfficeContainer, IHTTPRequest))
>>> breadcrumb = zope.component.getMultiAdapter((offices, request),
... interfaces.IBreadcrumb)
>>> breadcrumb.name
'Offices'
IBreadcrumbs
There is also a IBreadcrumbs adapter which knows how to collect breadcrumb
informations for each item he traverses. We need to setup a little bit of
infrastructure:
>>> root = rootFolder
>>> root['office'] = office
Register the IBreadcrumbs adapter:
>>> zope.component.provideAdapter(browser.Breadcrumbs,
... (zope.interface.Interface, zope.interface.Interface),
... interfaces.IBreadcrumbs)
Now we can collect breadcrumbs for our items. You can see the url is correct
and the label Zope Foundation is collected by the custom IBreadcrumb
adapter:
>>> breadcrumbs = zope.component.getMultiAdapter((office, request),
... interfaces.IBreadcrumbs)
>>> from pprint import pprint
>>> pprint(list(breadcrumbs.crumbs))
[{'activeURL': True,
'name': 'top',
'url': 'http://127.0.0.1'},
{'activeURL': True,
'name': 'Zope Foundation',
'url': 'http://127.0.0.1/office'}]
>>> breadcrumbs.__parent__ is office
True
Default breadcrumbs stops on virtual host root
>>> request._vh_root = office
>>> pprint(list(breadcrumbs.crumbs))
[{'activeURL': True,
'name': 'Zope Foundation',
'url': 'http://127.0.0.1'}]
If the breadcrumb of an item is a Null-adapter, then the item is ignored.
>>> from zope.traversing.interfaces import IContainmentRoot
>>> zope.component.provideAdapter(
... lambda c, r: None,
... (IContainmentRoot, IHTTPRequest),
... interfaces.IBreadcrumb)
>>> request = TestRequest()
>>> breadcrumbs = zope.component.getMultiAdapter(
... (office, request), interfaces.IBreadcrumbs)
>>> pprint(list(breadcrumbs.crumbs))
[{'activeURL': True,
'name': 'Zope Foundation',
'url': 'http://127.0.0.1/office'}]
CHANGES
3.1 (2025-02-14)
Add support for Python 3.12, 3.13.
Drop support for Python 3.7, 3.8.
3.0 (2023-02-24)
Drop support for Python 2.6, 2.7, 3.3, 3.4.
Add support for Python 3.7, 3.8, 3.9, 3.10, 3.11.
2.0.0a1 (2013-02-27)
Added support for Python 3.3.
Moved zope.app.testing dependency to zope.site.testing .
Moved zope.app.container dependency to zope.container .
Replaced deprecated zope.interface.implements usage with equivalent
zope.interface.implementer decorator.
Dropped support for Python 2.4 and 2.5.
1.0.1 (2007-01-21)
Bug: Test coverage brought up to 100%.
Feature: Register default IBreadcrumbs adapter.
Bug: Default IBreadcrumbs stops only on virtual host root, not on
ISite object
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages .
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names .
The dropdown lists show the available interpreters, ABIs, and platforms.
Enable javascript to be able to filter the list of wheel files.
Copy a direct link to the current filters
Copy
File name
Interpreter
Interpreter
py3
ABI
ABI
none
Platform
Platform
any
File details
Details for the file z3c_breadcrumb-4.0.tar.gz.
File metadata
Download URL: z3c_breadcrumb-4.0.tar.gz
Upload date:
Apr 14, 2025
Size: 10.6 kB
Tags: Source
Uploaded using Trusted Publishing? No
Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Hashes for z3c_breadcrumb-4.0.tar.gz
Algorithm
Hash digest
SHA256
d77d62b74be1f75995f9fa734802661bee6e736cc79009ac4f7c32c988f87f92
Copy
MD5
fedde03f8833a43b5798d0fddfb4228b
Copy
BLAKE2b-256
a0dfb1fa42f2a90cd5a86f7efb1b64f6759702ef0118c3e75d8f680eae26cf95
Copy
See more details on using hashes here.
File details
Details for the file z3c_breadcrumb-4.0-py3-none-any.whl.
File metadata
Download URL: z3c_breadcrumb-4.0-py3-none-any.whl
Upload date:
Apr 14, 2025
Size: 10.3 kB
Tags: Python 3
Uploaded using Trusted Publishing? No
Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Hashes for z3c_breadcrumb-4.0-py3-none-any.whl
Algorithm
Hash digest
SHA256
d5a409fa8e1b3615ec3b79a9c8f8c0af62445eaa8afb7bf7be90fa5c954cbe4d
Copy
MD5
9da06b5d6d99726649368677f2116a2b
Copy
BLAKE2b-256
7ce642b93abe2c6a8bdaae1e808c3eed75efbd9451855bcc53247f9b699c8ddf
Copy
See more details on using hashes here.