Content Providers¶
“Content Provider” is a term from the Java world that refers to components that can provide HTML content. It means nothing more! How the content is found and returned is totally up to the implementation. The Zope 3 touch to the concept is that content providers are multi-adapters that are looked up by the context, request (and thus the layer/skin), and view they are displayed in.
The second important concept of content providers are their two-phase rendering design. In the first phase the state of the content provider is prepared and, if applicable, any data the provider is responsible for is updated.
>>> from zope.contentprovider import interfaces
So let’s create a simple content provider:
>>> import zope.interface
>>> import zope.component
>>> from zope.publisher.interfaces import browser
>>> @zope.interface.implementer(interfaces.IContentProvider)
... @zope.component.adapter(zope.interface.Interface,
... browser.IDefaultBrowserLayer,
... zope.interface.Interface)
... class MessageBox(object):
... message = u'My Message'
...
... def __init__(self, context, request, view):
... self.__parent__ = view
...
... def update(self):
... pass
...
... def render(self):
... return u'<div class="box">%s</div>' % self.message
...
... def __repr__(self):
... return '<MessageBox object at %x>' % id(self)
The update()
method is executed during phase one. Since no state needs to
be calculated and no data is modified by this simple content provider, it is
an empty implementation. The render()
method implements phase 2 of the
process. We can now instantiate the content provider (manually) and render it:
>>> box = MessageBox(None, None, None)
>>> print(box.render())
<div class="box">My Message</div>
Since our content provider did not require the context, request or view to
create its HTML content, we were able to pass trivial dummy values into the
constructor. Also note that the provider must have a parent (using the
__parent__
attribute) specified at all times. The parent must be the view
the provider appears in.
I agree, this functionally does not seem very useful now. The constructor and
the update()
method seem useless and the returned content is totally
static. However, we implemented a contract for content providers that other
code can rely on. Content providers are (commonly) instantiated using the
context, request and view they appear in and are required to always generate
its HTML using those three components.
Two-Phased Content Providers¶
Let’s now have a look at a content provider that actively uses the two-phase rendering process. The simpler scenario is the case where the content provider updates a content component without affecting anything else. So let’s create a content component to be updated,
>>> class Article(object):
... title = u'initial'
>>> article = Article()
and the content provider that is updating the title:
>>> @zope.interface.implementer(interfaces.IContentProvider)
... @zope.component.adapter(zope.interface.Interface,
... browser.IDefaultBrowserLayer,
... zope.interface.Interface)
... class ChangeTitle(object):
... fieldName = 'ChangeTitle.title'
...
... def __init__(self, context, request, view):
... self.__parent__ = view
... self.context, self.request = context, request
...
... def update(self):
... if self.fieldName in self.request:
... self.context.title = self.request[self.fieldName]
...
... def render(self):
... return u'<input name="%s" value="%s" />' % (self.fieldName,
... self.context.title)
Using a request, let’s now instantiate the content provider and go through the two-phase rendering process:
>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> changer = ChangeTitle(article, request, None)
>>> changer.update()
>>> print(changer.render())
<input name="ChangeTitle.title" value="initial" />
Let’s now enter a new title and render the provider:
>>> request = TestRequest(form={'ChangeTitle.title': u'new title'})
>>> changer = ChangeTitle(article, request, None)
>>> changer.update()
>>> print(changer.render())
<input name="ChangeTitle.title" value="new title" />
>>> print(article.title)
new title
So this was easy. Let’s now look at a case where one content provider’s update influences the content of another. Let’s say we have a content provider that displays the article’s title:
>>> @zope.interface.implementer(interfaces.IContentProvider)
... @zope.component.adapter(zope.interface.Interface,
... browser.IDefaultBrowserLayer,
... zope.interface.Interface)
... class ViewTitle(object):
...
... def __init__(self, context, request, view):
... self.context, self.__parent__ = context, view
...
... def update(self):
... pass
...
... def render(self):
... return u'<h1>Title: %s</h1>' % self.context.title
Let’s now say that the ShowTitle
content provider is shown on a page
before the ChangeTitle
content provider. If we do the full rendering
process for each provider in sequence, we get the wrong result:
>>> request = TestRequest(form={'ChangeTitle.title': u'newer title'})
>>> viewer = ViewTitle(article, request, None)
>>> viewer.update()
>>> print(viewer.render())
<h1>Title: new title</h1>
>>> changer = ChangeTitle(article, request, None)
>>> changer.update()
>>> print(changer.render())
<input name="ChangeTitle.title" value="newer title" />
So the correct way of doing this is to first complete phase 1 (update) for all providers, before executing phase 2 (render):
>>> request = TestRequest(form={'ChangeTitle.title': u'newest title'})
>>> viewer = ViewTitle(article, request, None)
>>> changer = ChangeTitle(article, request, None)
>>> viewer.update()
>>> changer.update()
>>> print(viewer.render())
<h1>Title: newest title</h1>
>>> print(changer.render())
<input name="ChangeTitle.title" value="newest title" />
UpdateNotCalled
Errors¶
Since calling update
before any other method that
mutates the provider or any other data is so important to the correct
functioning of the API, the developer has the choice to raise the
UpdateNotCalled
error, if any method is called before
update()
(with exception of the constructor):
>>> @zope.interface.implementer(interfaces.IContentProvider)
... @zope.component.adapter(zope.interface.Interface,
... browser.IDefaultBrowserLayer,
... zope.interface.Interface)
... class InfoBox(object):
...
... def __init__(self, context, request, view):
... self.__parent__ = view
... self.__updated = False
...
... def update(self):
... self.__updated = True
...
... def render(self):
... if not self.__updated:
... raise interfaces.UpdateNotCalled
... return u'<div>Some information</div>'
>>> info = InfoBox(None, None, None)
>>> info.render()
Traceback (most recent call last):
...
UpdateNotCalled: ``update()`` was not called yet.
>>> info.update()
>>> print(info.render())
<div>Some information</div>
zope.contentprovider.interfaces¶
Content provider interfaces
-
interface
zope.contentprovider.interfaces.
IUpdateNotCalled
[source]¶ Extends:
zope.interface.common.interfaces.IRuntimeError
Update Not Called
An error that is raised when any content provider method is called before the
update()
method.
-
exception
zope.contentprovider.interfaces.
UpdateNotCalled
(*args)[source]¶ Bases:
exceptions.RuntimeError
Default implementation of
IUpdateNotCalled
.
-
interface
zope.contentprovider.interfaces.
IBeforeUpdateEvent
[source]¶ Extends:
zope.interface.interfaces.IObjectEvent
A Contentprovider will be updated
-
request
¶ The request in which the object is udpated, might also be None
-
-
class
zope.contentprovider.interfaces.
BeforeUpdateEvent
(provider, request=None)[source]¶ Bases:
zope.interface.interfaces.ObjectEvent
Default implementation of
IBeforeUpdateEvent
.
-
interface
zope.contentprovider.interfaces.
IContentProvider
[source]¶ Extends:
zope.browser.interfaces.IBrowserView
A piece of content to be shown on a page.
Objects implementing this interface are providing HTML content when they are rendered. It is up to the implementation to decide how to lookup necessary data to complete the job.
Content Providers use a two-stage process to fulfill their contract:
- The first stage is responsible to calculate the state of the content
provider and, if applicable, edit the data. This stage is executed
using the
update()
method. - During the second stage the provider constructs/renders its HTML
output based on the state that was calculated in the first stage. This
stage is executed using the
render()
method.
Content Providers are discriminated by three components: the context, the request and the view. This allows great control over the selection of the provider.
-
__parent__
¶ The view the provider appears in.
The view is the third discriminator of the content provider. It allows that the content can be controlled for different views.
Having it stored as the parent is also very important for the security context to be kept.
-
update
()¶ Initialize the content provider.
This method should perform all state calculation and not refer it to the rendering stage.
In this method, all state must be calculated from the current interaction (e.g., the browser request); all contained or managed content providers must have
update()
be called as well; any additional stateful API for contained or managed content providers must be handled; and persistent objects should be modified, if the provider is going to do it.Do not store state about persistent objects: the rendering process should actually use the persistent objects for the data, in case other components modify the object between the update and render stages.
This method must be called before any other method that mutates the instance (besides the class constructor). Non-mutating methods and attributes may raise an error if used before
update()
is called. The view may rely on this order but is not required to explicitly enforce this. Implementations may enforce it as a developer aid.
-
render
(*args, **kw)¶ Return the content provided by this content provider.
Calling this method before
update()
may (but is not required to) raise anUpdateNotCalled
error.
- The first stage is responsible to calculate the state of the content
provider and, if applicable, edit the data. This stage is executed
using the
-
interface
zope.contentprovider.interfaces.
IContentProviderType
[source]¶ Extends:
zope.interface.interfaces.IInterface
Type interface for content provider types (interfaces derived from IContentProvider).
-
interface
zope.contentprovider.interfaces.
ITALNamespaceData
[source]¶ Extends:
zope.interface.interfaces.IInterface
A type interface that marks an interface as a TAL data specification.
All fields specified in an interface that provides
ITALNamespaceData
will be looked up in the TAL context and stored on the content provider. A content provider can have multiple interfaces that are of this type.
-
exception
zope.contentprovider.interfaces.
ContentProviderLookupError
[source]¶ Bases:
zope.interface.interfaces.ComponentLookupError
No content provider was found.
-
interface
zope.contentprovider.interfaces.
ITALESProviderExpression
[source]¶ Extends:
zope.tales.interfaces.ITALESExpression
Return the HTML content of the named provider.
To call a content provider in a view use the the following syntax in a page template:
<tal:block replace="structure provider:provider.name">
The content provider is looked up by the (context, request, view) objects and the name (
provider.name
).