Skip to main content

Easier z3c.pagelet handling

Project description

Easy z3c.pagelet registration

The <gocept:pagelet> directive allows easier registration of z3c.pagelets. It behaves quite like <browser:page>.[1]_

Template only

It is possible to just use a template as pagelet. A class is not required:

>>> context = xmlconfig.string("""
... <configure
...     xmlns:gocept="http://namespaces.gocept.com/zcml">
...   <gocept:pagelet
...       name="index.html"
...       for="*"
...       permission="zope.Public"
...       template="test-template.pt"
...       />
... </configure>
... """, context)

We should now have a page:

>>> import zope.component
>>> from zope.publisher.browser import TestRequest
>>> pagelet = zope.component.getMultiAdapter(
...     (object, TestRequest()), name='index.html')
>>> pagelet
<gocept.pagelet.zcml.SimplePagelet object at 0x...>
>>> pagelet.__name__
u'index.html'

When we render the pagelet the test-template is used:

>>> pagelet.render()
u'Hello from the test template.\n'

Class only

Of course it’s also possible to register a class without a template. Create a class and make it available in a module:

>>> from z3c.pagelet.browser import BrowserPagelet
>>> class MyPagelet(BrowserPagelet):
...     """Custom pagelet"""
...     def render(self):
...         return u"Hello from the custom pagelet."""

Make it available under the fake package custom:

>>> sys.modules['custom'] = type(
...     'Module', (),
...     {'MyPagelet': MyPagelet})()

Make it available via ZCML:

>>> context = xmlconfig.string("""
... <configure
...     xmlns:gocept="http://namespaces.gocept.com/zcml">
...   <gocept:pagelet
...       name="class.html"
...       for="*"
...       permission="zope.Public"
...       class="custom.MyPagelet"
...       />
... </configure>
... """, context)

Get the pagelet:

>>> pagelet = zope.component.getMultiAdapter(
...     (object, TestRequest()), name='class.html')
>>> pagelet
<gocept.pagelet.zcml.MyPagelet object at 0x...>
>>> pagelet.render()
u'Hello from the custom pagelet.'

Class and template

It’s for course also possible to specify both class and template. So create another pagelet class and register it:

>>> class MyPagelet2(BrowserPagelet):
...     """Custom pagelet"""
...     i_am_very_custom = True
>>> sys.modules['custom'] = type(
...     'Module', (),
...     {'MyPagelet': MyPagelet2})()

Make it available via zcml:

>>> context = xmlconfig.string("""
... <configure
...     xmlns:gocept="http://namespaces.gocept.com/zcml">
...   <gocept:pagelet
...       name="class-template.html"
...       for="*"
...       permission="zope.Public"
...       class="custom.MyPagelet"
...       template="test-template.pt"
...       />
... </configure>
... """, context)
>>> pagelet = zope.component.getMultiAdapter(
...     (object, TestRequest()), name='class-template.html')
>>> pagelet
<gocept.pagelet.zcml.MyPagelet2 object at 0x...>
>>> pagelet.render()
u'Hello from the test template.\n'
>>> pagelet.i_am_very_custom
True

Changes

0.3 (2009-12-27)

  • Using zope.browserpage and zope.browsermenu instead of zope.app.publisher.

0.2 (2009-12-27)

  • Allow arbitrary number of context elements for adaptation.

0.1 (2008-09-20)

  • First public release.

Contributors

  • Michael Howitz <mh at gocept dot com>

  • Christian Theune <ct at gocept dot com>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gocept.pagelet-0.3.tar.gz (6.6 kB view details)

Uploaded Source

File details

Details for the file gocept.pagelet-0.3.tar.gz.

File metadata

  • Download URL: gocept.pagelet-0.3.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for gocept.pagelet-0.3.tar.gz
Algorithm Hash digest
SHA256 0c122b70c266a832ab879b18031145a98e5ddcbee3ce02941efff165b83d9404
MD5 42a45e917b0d470e5dd4f92d1d9575d7
BLAKE2b-256 35677964c3ef7e7bc33687dd5f29146ec2c1bc901280c207029b4e28a19a40be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page