/usr/share/pyshared/zope/viewlet/directives.txt is in python-zope.viewlet 3.7.2-0ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | ================================
The ``viewletManager`` Directive
================================
The ``viewletManager`` directive allows you to quickly register a new viewlet
manager without worrying about the details of the ``adapter``
directive. Before we can use the directives, we have to register their
handlers by executing the package's meta configuration:
>>> from zope.configuration import xmlconfig
>>> context = xmlconfig.string('''
... <configure i18n_domain="zope">
... <include package="zope.viewlet" file="meta.zcml" />
... </configure>
... ''')
Now we can register a viewlet manager:
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewletManager
... name="defaultmanager"
... permission="zope.Public"
... />
... </configure>
... ''', context=context)
Let's make sure the directive has really issued a sensible adapter
registration; to do that, we create some dummy content, request and view
objects:
>>> import zope.interface
>>> class Content(object):
... zope.interface.implements(zope.interface.Interface)
>>> content = Content()
>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> from zope.publisher.browser import BrowserView
>>> view = BrowserView(content, request)
Now let's lookup the manager. This particular registration is pretty boring:
>>> import zope.component
>>> from zope.viewlet import interfaces
>>> manager = zope.component.getMultiAdapter(
... (content, request, view),
... interfaces.IViewletManager, name='defaultmanager')
>>> manager
<zope.viewlet.manager.<ViewletManager providing IViewletManager> object ...>
>>> interfaces.IViewletManager.providedBy(manager)
True
>>> manager.template is None
True
>>> manager.update()
>>> manager.render()
u''
However, this registration is not very useful, since we did specify a specific
viewlet manager interface, a specific content interface, specific view or
specific layer. This means that all viewlets registered will be found.
The first step to effectively using the viewlet manager directive is to define
a special viewlet manager interface:
>>> class ILeftColumn(interfaces.IViewletManager):
... """Left column of my page."""
Now we can register register a manager providing this interface:
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewletManager
... name="leftcolumn"
... permission="zope.Public"
... provides="zope.viewlet.directives.ILeftColumn"
... />
... </configure>
... ''', context=context)
>>> manager = zope.component.getMultiAdapter(
... (content, request, view), ILeftColumn, name='leftcolumn')
>>> manager
<zope.viewlet.manager.<ViewletManager providing ILeftColumn> object ...>
>>> ILeftColumn.providedBy(manager)
True
>>> manager.template is None
True
>>> manager.update()
>>> manager.render()
u''
Next let's see what happens, if we specify a template for the viewlet manager:
>>> import os, tempfile
>>> temp_dir = tempfile.mkdtemp()
>>> leftColumnTemplate = os.path.join(temp_dir, 'leftcolumn.pt')
>>> open(leftColumnTemplate, 'w').write('''
... <div class="column">
... <div class="entry"
... tal:repeat="viewlet options/viewlets"
... tal:content="structure viewlet" />
... </div>
... ''')
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewletManager
... name="leftcolumn"
... permission="zope.Public"
... provides="zope.viewlet.directives.ILeftColumn"
... template="%s"
... />
... </configure>
... ''' %leftColumnTemplate, context=context)
>>> manager = zope.component.getMultiAdapter(
... (content, request, view), ILeftColumn, name='leftcolumn')
>>> manager
<zope.viewlet.manager.<ViewletManager providing ILeftColumn> object ...>
>>> ILeftColumn.providedBy(manager)
True
>>> manager.template
<BoundPageTemplateFile of ...<ViewletManager providing ILeftColumn> ...>>
>>> manager.update()
>>> print manager.render().strip()
<div class="column">
</div>
Additionally you can specify a class that will serve as a base to the default
viewlet manager or be a viewlet manager in its own right. In our case we will
provide a custom implementation of the ``sort()`` method, which will sort by a
weight attribute in the viewlet:
>>> class WeightBasedSorting(object):
... def sort(self, viewlets):
... return sorted(viewlets,
... lambda x, y: cmp(x[1].weight, y[1].weight))
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewletManager
... name="leftcolumn"
... permission="zope.Public"
... provides="zope.viewlet.directives.ILeftColumn"
... template="%s"
... class="zope.viewlet.directives.WeightBasedSorting"
... />
... </configure>
... ''' %leftColumnTemplate, context=context)
>>> manager = zope.component.getMultiAdapter(
... (content, request, view), ILeftColumn, name='leftcolumn')
>>> manager
<zope.viewlet.manager.<ViewletManager providing ILeftColumn> object ...>
>>> manager.__class__.__bases__
(<class 'zope.viewlet.directives.WeightBasedSorting'>,
<class 'zope.viewlet.manager.ViewletManagerBase'>)
>>> ILeftColumn.providedBy(manager)
True
>>> manager.template
<BoundPageTemplateFile of ...<ViewletManager providing ILeftColumn> ...>>
>>> manager.update()
>>> print manager.render().strip()
<div class="column">
</div>
Finally, if a non-existent template is specified, an error is raised:
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewletManager
... name="leftcolumn"
... permission="zope.Public"
... template="foo.pt"
... />
... </configure>
... ''', context=context)
Traceback (most recent call last):
...
ZopeXMLConfigurationError: File "<string>", line 3.2-7.8
ConfigurationError: ('No such file', '...foo.pt')
=========================
The ``viewlet`` Directive
=========================
Now that we have a viewlet manager, we have to register some viewlets for
it. The ``viewlet`` directive is similar to the ``viewletManager`` directive,
except that the viewlet is also registered for a particular manager interface,
as seen below:
>>> weatherTemplate = os.path.join(temp_dir, 'weather.pt')
>>> open(weatherTemplate, 'w').write('''
... <div>sunny</div>
... ''')
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewlet
... name="weather"
... manager="zope.viewlet.directives.ILeftColumn"
... template="%s"
... permission="zope.Public"
... extra_string_attributes="can be specified"
... />
... </configure>
... ''' % weatherTemplate, context=context)
If we look into the adapter registry, we will find the viewlet:
>>> viewlet = zope.component.getMultiAdapter(
... (content, request, view, manager), interfaces.IViewlet,
... name='weather')
>>> viewlet.render().strip()
u'<div>sunny</div>'
>>> viewlet.extra_string_attributes
u'can be specified'
The manager now also gives us the output of the one and only viewlet:
>>> manager.update()
>>> print manager.render().strip()
<div class="column">
<div class="entry">
<div>sunny</div>
</div>
</div>
Let's now ensure that we can also specify a viewlet class:
>>> class Weather(object):
... weight = 0
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewlet
... name="weather2"
... for="*"
... manager="zope.viewlet.directives.ILeftColumn"
... template="%s"
... class="zope.viewlet.directives.Weather"
... permission="zope.Public"
... />
... </configure>
... ''' % weatherTemplate, context=context)
>>> viewlet = zope.component.getMultiAdapter(
... (content, request, view, manager), interfaces.IViewlet,
... name='weather2')
>>> viewlet().strip()
u'<div>sunny</div>'
Okay, so the template-driven cases work. But just specifying a class should
also work:
>>> class Sport(object):
... weight = 0
... def __call__(self):
... return u'Red Sox vs. White Sox'
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewlet
... name="sport"
... for="*"
... manager="zope.viewlet.directives.ILeftColumn"
... class="zope.viewlet.directives.Sport"
... permission="zope.Public"
... />
... </configure>
... ''', context=context)
>>> viewlet = zope.component.getMultiAdapter(
... (content, request, view, manager), interfaces.IViewlet, name='sport')
>>> viewlet()
u'Red Sox vs. White Sox'
It should also be possible to specify an alternative attribute of the class to
be rendered upon calling the viewlet:
>>> class Stock(object):
... weight = 0
... def getStockTicker(self):
... return u'SRC $5.19'
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewlet
... name="stock"
... for="*"
... manager="zope.viewlet.directives.ILeftColumn"
... class="zope.viewlet.directives.Stock"
... attribute="getStockTicker"
... permission="zope.Public"
... />
... </configure>
... ''', context=context)
>>> viewlet = zope.component.getMultiAdapter(
... (content, request, view, manager), interfaces.IViewlet,
... name='stock')
>>> viewlet.render()
u'SRC $5.19'
A final feature the ``viewlet`` directive is that it supports the
specification of any number of keyword arguments:
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewlet
... name="stock2"
... permission="zope.Public"
... class="zope.viewlet.directives.Stock"
... weight="8"
... />
... </configure>
... ''', context=context)
>>> viewlet = zope.component.getMultiAdapter(
... (content, request, view, manager), interfaces.IViewlet,
... name='stock2')
>>> viewlet.weight
u'8'
Error Scenarios
---------------
Neither the class or template have been specified:
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewlet
... name="testviewlet"
... manager="zope.viewlet.directives.ILeftColumn"
... permission="zope.Public"
... />
... </configure>
... ''', context=context)
Traceback (most recent call last):
...
ZopeXMLConfigurationError: File "<string>", line 3.2-7.8
ConfigurationError: Must specify a class or template
The specified attribute is not ``__call__``, but also a template has been
specified:
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewlet
... name="testviewlet"
... manager="zope.viewlet.directives.ILeftColumn"
... template="test_viewlet.pt"
... attribute="faux"
... permission="zope.Public"
... />
... </configure>
... ''', context=context)
Traceback (most recent call last):
...
ZopeXMLConfigurationError: File "<string>", line 3.2-9.8
ConfigurationError: Attribute and template cannot be used together.
Now, we are not specifying a template, but a class that does not have the
specified attribute:
>>> context = xmlconfig.string('''
... <configure xmlns="http://namespaces.zope.org/browser" i18n_domain="zope">
... <viewlet
... name="testviewlet"
... manager="zope.viewlet.directives.ILeftColumn"
... class="zope.viewlet.directives.Sport"
... attribute="faux"
... permission="zope.Public"
... />
... </configure>
... ''', context=context)
Traceback (most recent call last):
...
ZopeXMLConfigurationError: File "<string>", line 3.2-9.8
ConfigurationError: The provided class doesn't have the specified attribute
Cleanup
-------
>>> import shutil
>>> shutil.rmtree(temp_dir)
|