1 | Breadcrumbs |
---|
2 | *********** |
---|
3 | |
---|
4 | We use an adapter approach to get a flexible system of creating and |
---|
5 | retrieving breadcrumbs for certain views. |
---|
6 | |
---|
7 | |
---|
8 | How to get existing breadcrumbs |
---|
9 | =============================== |
---|
10 | |
---|
11 | We create a university to check the breadcrumb functionality. |
---|
12 | |
---|
13 | >>> from waeup.kofa.app import University |
---|
14 | >>> root = getRootFolder() |
---|
15 | >>> root['app'] = University() |
---|
16 | >>> app = root['app'] |
---|
17 | |
---|
18 | The quick way (if you have a viewname) |
---|
19 | -------------------------------------- |
---|
20 | |
---|
21 | We can get an ordered list of breadcrumbs for a given context and |
---|
22 | viewname: |
---|
23 | |
---|
24 | >>> from waeup.kofa.browser.breadcrumbs import getBreadcrumbList |
---|
25 | >>> blist1 = getBreadcrumbList(app, 'index') |
---|
26 | >>> blist1 |
---|
27 | [<waeup.kofa.browser.breadcrumbs.UniversityBreadcrumb object at 0x...>] |
---|
28 | |
---|
29 | A slightly more extensive list for the datacenter: |
---|
30 | |
---|
31 | >>> blist2 = getBreadcrumbList(app['datacenter'], 'index') |
---|
32 | >>> from pprint import pprint |
---|
33 | >>> pprint(blist2) |
---|
34 | [<waeup.kofa.browser.breadcrumbs.UniversityBreadcrumb object at 0x...>, |
---|
35 | <waeup.kofa.browser.breadcrumbs.AdministrationBreadcrumb object at 0x...>, |
---|
36 | <waeup.kofa.browser.breadcrumbs.DataCenterBreadcrumb object at 0x...>] |
---|
37 | |
---|
38 | We get a breadcrumb for university, administration area and data |
---|
39 | center in that order. |
---|
40 | |
---|
41 | Each breadcrumb provides a ``title``, ``context``, and ``viewname`` |
---|
42 | attribute, so that views and page templates can easily create markup |
---|
43 | out of it: |
---|
44 | |
---|
45 | >>> pprint([(x.title) for x in blist2]) |
---|
46 | [u'Home', u'Administration', u'Data Center'] |
---|
47 | |
---|
48 | >>> pprint([(x.context, x.viewname) for x in blist2]) |
---|
49 | [(<waeup.kofa.app.University object at 0x...>, 'index'), |
---|
50 | (<waeup.kofa.app.University object at 0x...>, 'administration'), |
---|
51 | (<waeup.kofa.datacenter.DataCenter object at 0x...>, 'index')] |
---|
52 | |
---|
53 | The administration area breadcrumb might be a surprise, as there is no |
---|
54 | equivalent object in the ZODB. In fact the administration area is only |
---|
55 | a certain view (the 'administration' view) on the university object. |
---|
56 | |
---|
57 | We will show below, how you can define breadcrumbs this way. |
---|
58 | |
---|
59 | As a rule of thumb you should know, that every breadcrumb is not only |
---|
60 | attached for a certain kind of object, but for a tuple of context |
---|
61 | object and a viewname. |
---|
62 | |
---|
63 | This way we can make sure, that there are different breadcrumb lists |
---|
64 | generated for instance for the administration view of University |
---|
65 | instances and the index view. While the first should look something |
---|
66 | like:: |
---|
67 | |
---|
68 | Home -> Administration |
---|
69 | |
---|
70 | we expect for the latter only:: |
---|
71 | |
---|
72 | Home |
---|
73 | |
---|
74 | |
---|
75 | Another quick way (if you have a complete view) |
---|
76 | ----------------------------------------------- |
---|
77 | |
---|
78 | Another quick approach for use in viewlets, views and similar |
---|
79 | contexts, where you usually have a view available but not a viewname, |
---|
80 | is to use ``getBreadcrumbListForView``. |
---|
81 | |
---|
82 | We create a view (a page in this case, but pages are views) on the |
---|
83 | user administration index page: |
---|
84 | |
---|
85 | >>> from zope.publisher.browser import TestRequest |
---|
86 | >>> from zope.component import getMultiAdapter |
---|
87 | >>> page = getMultiAdapter((app['users'], TestRequest()), name='index') |
---|
88 | |
---|
89 | Now we can get the breadcrumbs for this view: |
---|
90 | |
---|
91 | >>> from waeup.kofa.browser.breadcrumbs import getBreadcrumbListForView |
---|
92 | >>> blist3 = getBreadcrumbListForView(page) |
---|
93 | >>> [x.title for x in blist3] |
---|
94 | [u'Home', u'Administration', u'Officers'] |
---|
95 | |
---|
96 | ..note:: This works only for views created with grokcore.component |
---|
97 | (i.e. Grok Views, Pages, etc.) but not for simple Zope3 |
---|
98 | views, as the viewname can not easily be extracted from the |
---|
99 | view for the latter ones. |
---|
100 | |
---|
101 | |
---|
102 | Type-safe retrieval of breadcrumbs for a view |
---|
103 | --------------------------------------------- |
---|
104 | |
---|
105 | If you want to make sure, that a view for which you want to get |
---|
106 | breadcrumbs is really a Grok view, then you can use the |
---|
107 | IBreadcrumbContainer adapter. This one adapts only grok views. |
---|
108 | |
---|
109 | The returned breadcrumb container supports iteration: |
---|
110 | |
---|
111 | >>> from waeup.kofa.browser.interfaces import IBreadcrumbContainer |
---|
112 | >>> mybccontainer = IBreadcrumbContainer(page) |
---|
113 | >>> [x.title for x in mybccontainer] |
---|
114 | [u'Home', u'Administration', u'Officers'] |
---|
115 | |
---|
116 | It is, however, not a real Python list but only an iterable |
---|
117 | container. You can also get the real list of breadcrumbs: |
---|
118 | |
---|
119 | >>> pprint(mybccontainer.getList()) |
---|
120 | [<...breadcrumbs.UniversityBreadcrumb object at 0x...>, |
---|
121 | <...breadcrumbs.AdministrationBreadcrumb object at 0x...>, |
---|
122 | <...breadcrumbs.UsersContainerBreadcrumb object at 0x...>] |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | The general details |
---|
127 | ------------------- |
---|
128 | |
---|
129 | Now we can get breadcrumbs for contexts and view names. For example a |
---|
130 | breadcrumb for the 'index' view of our University object: |
---|
131 | |
---|
132 | >>> from zope.component import getAdapter |
---|
133 | >>> from waeup.kofa.browser.interfaces import IBreadcrumb |
---|
134 | >>> b1 = getAdapter(app, IBreadcrumb, 'index') |
---|
135 | >>> b1 |
---|
136 | <waeup.kofa.browser.breadcrumbs.UniversityBreadcrumb object at 0x...> |
---|
137 | |
---|
138 | Breadcrumb objects provide a title: |
---|
139 | |
---|
140 | >>> b1.title |
---|
141 | u'Home' |
---|
142 | |
---|
143 | a parent (None for the app object): |
---|
144 | |
---|
145 | >>> b1.parent is None |
---|
146 | True |
---|
147 | |
---|
148 | and a viewname: |
---|
149 | |
---|
150 | >>> b1.viewname |
---|
151 | 'index' |
---|
152 | |
---|
153 | Using a context and a viewname, one can easily compute a complete URL |
---|
154 | in views, viewlets, etc. |
---|
155 | |
---|
156 | >>> b2 = getAdapter(app['datacenter'], IBreadcrumb, 'index') |
---|
157 | >>> b2 |
---|
158 | <waeup.kofa.browser.breadcrumbs.DataCenterBreadcrumb object at 0x...> |
---|
159 | |
---|
160 | >>> b2.title |
---|
161 | u'Data Center' |
---|
162 | |
---|
163 | >>> b2.viewname |
---|
164 | 'index' |
---|
165 | |
---|
166 | As the data center is not root of the hierarchy, it provides a |
---|
167 | non-trivial parent: |
---|
168 | |
---|
169 | >>> b2.parent |
---|
170 | (<waeup.kofa.app.University object at 0x...>, 'administration') |
---|
171 | |
---|
172 | This result denotes a new context object (the University instance we |
---|
173 | created above) and a view name ('administration'). |
---|
174 | |
---|
175 | Normally, the view name would be 'index', but as some administrative |
---|
176 | components should look to the user as if they were grouped in a common |
---|
177 | subarea of the root (the administration area), we can also set a |
---|
178 | different view than the default as target. |
---|
179 | |
---|
180 | When we like to receive the breadcrumb for this parent, we can do so |
---|
181 | as above: |
---|
182 | |
---|
183 | >>> context, viewname = b2.parent |
---|
184 | >>> b3 = getAdapter(context, IBreadcrumb, viewname) |
---|
185 | >>> b3 |
---|
186 | <waeup.kofa...breadcrumbs.AdministrationBreadcrumb object at 0x...> |
---|
187 | |
---|
188 | As you can see, we get an AdministrationBreadcrumb, although the |
---|
189 | context object, for which the breadcrumb was created is also the |
---|
190 | University instance as above: |
---|
191 | |
---|
192 | >>> b3.context is b1.context |
---|
193 | True |
---|
194 | |
---|
195 | This administration area now has still a parent, the front-page: |
---|
196 | |
---|
197 | >>> context, viewname = b3.parent |
---|
198 | >>> context, viewname |
---|
199 | (<waeup.kofa.app.University object at 0x...>, 'index') |
---|
200 | |
---|
201 | We create last breadcrumb: |
---|
202 | |
---|
203 | >>> b4 = getAdapter(context, IBreadcrumb, viewname) |
---|
204 | |
---|
205 | This is finally the last piece, the breadcrumb which links to the |
---|
206 | front page. It has no parent: |
---|
207 | |
---|
208 | >>> b4.parent is None |
---|
209 | True |
---|
210 | |
---|
211 | |
---|
212 | How to create new breadcrumbs |
---|
213 | ============================= |
---|
214 | |
---|
215 | Breadcrumbs are looked up as named adapters. This means, you must |
---|
216 | create a named adapter with the following specifications: |
---|
217 | |
---|
218 | * Derive your breadcrumb class from ``Breadcrumb`` class. This will |
---|
219 | save you lots of work. |
---|
220 | |
---|
221 | * The adapter context (``grok.context``) must be the object type you |
---|
222 | want to provide a breadcrumb for. So, if you want to write a |
---|
223 | Breadcrumb adapter for objects providing ``IMyFunnyType``, write:: |
---|
224 | |
---|
225 | grok.context(IMyFunnyType) |
---|
226 | |
---|
227 | * The adapter name (``grok.name``) must be the same as the name of |
---|
228 | the view, for which the breadcrumb component should be |
---|
229 | registered. |
---|
230 | |
---|
231 | If you want to write a breadcrumb component for the 'index' view of |
---|
232 | some content type and derive your class from Breadcrumb, then you |
---|
233 | can skip this step as 'index' is the default name. |
---|
234 | |
---|
235 | * Set a ``title`` attribute to give the text that should be |
---|
236 | displayed. Use something like:: |
---|
237 | |
---|
238 | @property |
---|
239 | def title(self): |
---|
240 | return self.some_computed_title() |
---|
241 | |
---|
242 | if you want computed titles. |
---|
243 | |
---|