Skip to main content

PageTemplate integration for Zope 3

Project description

The zope.app.pagetemplate package integrates the Page Template templating system (zope.pagetemplate) into the Zope 3 application server. In particular, it provides:

  • a TALES engine implementation that uses Zope’s security system for checking access,

  • a ViewPageTemplateFile class that can be put on a browser page class as an attribute and will function as a callable method whose result is the rendered template, e.g.:

    from zope.publisher import BrowserPage
    from zope.app.pagetemplate import ViewPageTemplateFile
    
    class HelloWorldPage(BrowserPage):
        __call__ = ViewPageTemplateFile('helloworld.pt')
  • TALES namespace adapters for easy access to DublinCore metadata (e.g. obj/zope:title) and URL quoting (e.g. obj/@@absolute_url/url:quote).

Macro extension

This test demonstrates how macro extension allows a macro to extend and re-offer a slot for a client template to fill. This is likely not the best place for this test, but it demonstrates how to use the macro extension feature in context.

Let’s look at our test view using the root folder to make sure we’re seeing the expected template expansion:

>>> print http("""
... GET /@@inner HTTP/1.1
... Authorization: Basic mgr:mgrpw
... """, handle_errors=False)
HTTP/1.1 200 ...
<BLANKLINE>
<html>
<head>
<title>Example: outer</title>
</head>
<body>
hello
<div>
<div>
inner body slot content
</div>
intermediate body slot stuff
</div>
</body>
</html>
<BLANKLINE>

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.app.pagetemplate import ViewPageTemplateFile
>>> from zope.app.pagetemplate.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.app.pagetemplate.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>

Changes

3.7.1 (2009-05-27)

  • Restored zope.app.pagetemplate.engine module, using BBB imports from zope.pagetemplate.engine.

3.7.0 (2009-05-25)

  • Moved the engine module and associated testing machinery to zope.pagetemplate (version 3.5.0).

3.6.0 (2009-05-18)

  • Moved namedtemplate.* from zope.formlib here as it is more about a page template engine than a formular library. This also breaks some dependencies on zope.formlib.

  • Added doctests to long_description to show up on pypi.

3.5.0 (2009-02-01)

  • Use zope.container instead of zope.app.container.

3.4.1 (2008-07-30)

  • Substitute zope.app.zapi by direct calls to its wrapped apis. See http://launchpad.net/bugs/219302

  • Fix deprecation warning in ftesting.zcml: ZopeSecurityPolicy now lives in zope.securitypolicy.

3.4.0 (2007-09-28)

  • Initial release as standalone package.

  • Dependency on zope.app.interpreter moved to an extra [inline-evaluation]. It is only needed by zope.app.pythonpage, which is an oddity.

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

zope.app.pagetemplate-3.7.1.tar.gz (18.1 kB view hashes)

Uploaded Source

Supported by

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