/usr/share/pyshared/zope/browserpage/namedtemplate.txt is in python-zope.browserpage 3.12.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 | ===============
Named Templates
===============
We often want to be able to define view logic and view templates
independently. We'd like to be able to change the template used by a
form without being forced to modify the form.
Named templates provide templates that are registered as named view
adapters. To define a named template, use the `NamedTemplateImplementation`
constructor:
>>> from zope.browserpage import ViewPageTemplateFile
>>> from zope.browserpage.namedtemplate import (
... NamedTemplateImplementation)
>>> sample = ViewPageTemplateFile('tests/namedtemplate.pt')
>>> sample = NamedTemplateImplementation(sample)
Let's define a view that uses the named template. To use a named
template, use the NamedTemplate constructor, and give a template name:
>>> from zope.browserpage.namedtemplate import NamedTemplate
>>> class MyView:
... def __init__(self, context, request):
... self.context = context
... self.request = request
...
... __call__ = NamedTemplate('sample')
Normally, we'd register a named template for a view interface, to
allow it to be registered for multiple views. We'll just register it
for our view class.
>>> from zope import component
>>> component.provideAdapter(sample, [MyView], name='sample')
Now, with this in place, we should be able to use our view:
>>> class MyContent:
... def __init__(self, name):
... self.name = name
>>> from zope.publisher.browser import TestRequest
>>> print MyView(MyContent('bob'), TestRequest())(x=42)
<html><body>
Hello bob
The URL is http://127.0.0.1
The positional arguments were ()
The keyword argument x is 42
</body></html>
<BLANKLINE>
The view type that a named template is to be used for can be supplied
when the named template is created:
>>> class MyView:
... def __init__(self, context, request):
... self.context = context
... self.request = request
...
... __call__ = NamedTemplate('sample2')
>>> sample = ViewPageTemplateFile('tests/namedtemplate.pt')
>>> sample = NamedTemplateImplementation(sample, MyView)
>>> component.provideAdapter(sample, name='sample2')
>>> print MyView(MyContent('bob'), TestRequest())(x=42)
<html><body>
Hello bob
The URL is http://127.0.0.1
The positional arguments were ()
The keyword argument x is 42
</body></html>
<BLANKLINE>
|