Skip to main content

Compatibility-layer for Zope Page Template engines.

Project description

Overview

This package implements a compatibility-layer between the following Zope Page Template engines:

  • z3c.pt

  • zope.pagetemplate

If the environment-variable PREFER_Z3C_PT is set to a true value, the z3c.pt engine will be used instead of zope.pagetemplate.

Note: This package superseeds and replaces the old z3c.pt.compat, please use z3c.ptcompat instead.

Usage

When writing new code or modifying existing code, import page template classes from the z3c.ptcompat package:

>>> from z3c.ptcompat import PageTemplateFile
>>> from z3c.ptcompat import ViewPageTemplateFile

Two methods are available to bind templates and template macros to a view:

>>> from z3c.ptcompat import bind_template
>>> from z3c.ptcompat import bind_macro

Both function return render-methods that accept keyword arguments which will be passed to the template.

>>> render = bind_template(template, view)
>>> render = bind_macro(template, view, request, macro)

Patches

By loading the patches module, Zope view page template classes will be monkey-patched to use the z3c.pt rendering facilities:

<include package=”z3c.ptcompat.patches” />

This is an alternative to changing module import locations.

Directives

We’ll use a configuration machine to carry out actions; note that when actions are executed, the configuration machine is emptied.

>>> from zope.configuration import config
>>> context = config.ConfigurationMachine()

Pages

>>> from z3c.ptcompat import zcml
>>> zcml.page_directive(
...    context, "test1", "zope.View", None,
...    template=os.path.join(path, "test.pt"))
>>> context.execute_actions()

Verify that page has been registered as a component.

>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> factory = component.getSiteManager().adapters.lookup(
...     (interface.Interface, IDefaultBrowserLayer),
...     interface.Interface, name="test1")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>

We can base the view on an existing class.

>>> class View(object):
...     pass
>>> zcml.page_directive(
...    context, "test2", "zope.View", None,
...    class_=View, template=os.path.join(path, "test.pt"))
>>> context.execute_actions()
>>> factory = component.getSiteManager().adapters.lookup(
...     (interface.Interface, IDefaultBrowserLayer),
...     interface.Interface, name="test2")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>
>>> issubclass(factory, View)
True

Forms

Define a simple schema that we will use for those directives:

>>> from zope.interface import Interface
>>> from zope.schema import TextLine
>>> class Schema(Interface):
...     title = TextLine(title=u'Some Title')

Add Form

>>> from z3c.ptcompat import zcml
>>> zcml.AddFormDirective(
...    context, name="add-test1", permission="zope.View",
...    schema=Schema, for_=Schema,
...    template=os.path.join(path, "test.pt"))()
>>> context.execute_actions()

Verify that page has been registered as a component.

>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> factory = component.getSiteManager().adapters.lookup(
...     (Schema, IDefaultBrowserLayer),
...     interface.Interface, name="add-test1")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>

We can base the view on an existing class.

>>> class View(object):
...     pass
>>> zcml.AddFormDirective(
...    context, name="add-test2", permission="zope.View",
...    schema=Schema, for_=Schema,
...    class_=View, template=os.path.join(path, "test.pt"))()
>>> context.execute_actions()
>>> factory = component.getSiteManager().adapters.lookup(
...     (Schema, IDefaultBrowserLayer),
...     interface.Interface, name="add-test2")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>
>>> issubclass(factory, View)
True

Edit Form

>>> from z3c.ptcompat import zcml
>>> zcml.EditFormDirective(
...    context, name="edit-test1", permission="zope.View",
...    schema=Schema, for_=Schema,
...    template=os.path.join(path, "test.pt"))()
>>> context.execute_actions()

Verify that page has been registered as a component.

>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> factory = component.getSiteManager().adapters.lookup(
...     (Schema, IDefaultBrowserLayer),
...     interface.Interface, name="edit-test1")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>

We can base the view on an existing class.

>>> class View(object):
...     pass
>>> zcml.EditFormDirective(
...    context, name="edit-test2", permission="zope.View",
...    schema=Schema, for_=Schema,
...    class_=View, template=os.path.join(path, "test.pt"))()
>>> context.execute_actions()
>>> factory = component.getSiteManager().adapters.lookup(
...     (Schema, IDefaultBrowserLayer),
...     interface.Interface, name="edit-test2")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>
>>> issubclass(factory, View)
True

Subedit Form

>>> from z3c.ptcompat import zcml
>>> zcml.SubeditFormDirective(
...    context, name="subedit-test1", permission="zope.View",
...    schema=Schema, for_=Schema,
...    template=os.path.join(path, "test.pt"))()
>>> context.execute_actions()

Verify that page has been registered as a component.

>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> factory = component.getSiteManager().adapters.lookup(
...     (Schema, IDefaultBrowserLayer),
...     interface.Interface, name="subedit-test1")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>

We can base the view on an existing class.

>>> class View(object):
...     pass
>>> zcml.SubeditFormDirective(
...    context, name="subedit-test2", permission="zope.View",
...    schema=Schema, for_=Schema,
...    class_=View, template=os.path.join(path, "test.pt"))()
>>> context.execute_actions()
>>> factory = component.getSiteManager().adapters.lookup(
...     (Schema, IDefaultBrowserLayer),
...     interface.Interface, name="subedit-test2")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>
>>> issubclass(factory, View)
True

Form

We can base the view on an existing class.

>>> class View(object):
...     pass
>>> zcml.FormDirective(
...    context, name="form-test2", permission="zope.View",
...    schema=Schema, for_=Schema,
...    class_=View, template=os.path.join(path, "test.pt"))()
>>> context.execute_actions()
>>> factory = component.getSiteManager().adapters.lookup(
...     (Schema, IDefaultBrowserLayer),
...     interface.Interface, name="form-test2")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>
>>> issubclass(factory, View)
True

Schema Display

>>> from z3c.ptcompat import zcml
>>> zcml.SchemaDisplayDirective(
...    context, name="schema-display-test1", permission="zope.View",
...    schema=Schema, for_=Schema,
...    template=os.path.join(path, "test.pt"))()
>>> context.execute_actions()

Verify that page has been registered as a component.

>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> factory = component.getSiteManager().adapters.lookup(
...     (Schema, IDefaultBrowserLayer),
...     interface.Interface, name="schema-display-test1")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>

We can base the view on an existing class.

>>> class View(object):
...     pass
>>> zcml.SchemaDisplayDirective(
...    context, name="schema-display-test2", permission="zope.View",
...    schema=Schema, for_=Schema,
...    class_=View, template=os.path.join(path, "test.pt"))()
>>> context.execute_actions()
>>> factory = component.getSiteManager().adapters.lookup(
...     (Schema, IDefaultBrowserLayer),
...     interface.Interface, name="schema-display-test2")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>
>>> issubclass(factory, View)
True

Viewlet managers

>>> zcml.viewlet_manager_directive(
...    context, "test1", "zope.View",
...    template=os.path.join(path, "test.pt"))
>>> context.execute_actions()

Verify that page has been registered as a component.

>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> from zope.publisher.interfaces.browser import IBrowserView
>>> from zope.viewlet.interfaces import IViewletManager
>>> factory = component.getSiteManager().adapters.lookup(
...     (interface.Interface, IDefaultBrowserLayer, IBrowserView),
...     IViewletManager, name="test1")
>>> factory.template
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>

We can base the viewlet manager on an existing class.

>>> class ViewletManager(object):
...     pass
>>> zcml.viewlet_manager_directive(
...    context, "test2", "zope.View",
...    class_=ViewletManager, template=os.path.join(path, "test.pt"))
>>> context.execute_actions()

Verify that page has been registered as a component.

>>> factory = component.getSiteManager().adapters.lookup(
...     (interface.Interface, IDefaultBrowserLayer, IBrowserView),
...     IViewletManager, name="test2")
>>> factory.template
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>
>>> issubclass(factory, ViewletManager)
True

Viewlets

>>> zcml.viewlet_directive(
...    context, "test1", "zope.View",
...    template=os.path.join(path, "test.pt"))
>>> context.execute_actions()

Verify that page has been registered as a component.

>>> factory = component.getSiteManager().adapters.lookup(
...     (interface.Interface, IDefaultBrowserLayer, IBrowserView, IViewletManager),
...     interface.Interface, name="test1")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>

We can base the viewlet on an existing class.

>>> class Viewlet(object):
...     pass
>>> zcml.viewlet_directive(
...    context, "test2", "zope.View",
...    class_=Viewlet, template=os.path.join(path, "test.pt"))
>>> context.execute_actions()

Verify that page has been registered as a component.

>>> factory = component.getSiteManager().adapters.lookup(
...     (interface.Interface, IDefaultBrowserLayer, IBrowserView, IViewletManager),
...     interface.Interface, name="test2")
>>> factory.index
<ViewPageTemplateFile .../z3c/ptcompat/tests/test.pt>
>>> issubclass(factory, Viewlet)
True

Changelog

0.5.6 (2010-04-19)

  • Remove broken templates from registry during engine migration. In some testing situation, stale templates would be tracked in the regitry.

  • Existing template instances are now migrated to the right engine when using the enable and disable methods. [malthe]

0.5.5 (2009-07-24)

  • Make tests pass in a binary release by not relying on the pacakge structure.

0.5.4 (2009-07-23)

  • Added a test requirement explicitely.

0.5.3 (2009-05-28)

  • Added support for browser:addform, browser:editform, browser:form, and browser:schemadisplay directives.

0.5.2 (2009-03-09)

  • Fixing brown-bag release 0.5.1.

0.5.1 (2009-03-09)

  • Added missing lxml test dependency.

  • Fixed tests to work with current version of z3c.pt.

  • Fixed autor e-mail address.

  • Added doctests and change log to long description to show up at pypi homepage.

  • Reformatted release dates in change log to use iso dates.

0.5 (2009-02-16)

  • Added module which patches zope.app.pagetemplate such that template classes depend on z3c.pt for rendering (import optional). [malthe]

0.4 (2009-02-10)

  • Rename project to z3c.ptcompat to deal with setuptools issues (as discussed on zope-dev http://mail.zope.org/pipermail/zope-dev/2008-December/033820.html)

  • Added optional option doctest for output checker to allow usage with alternative implementations, e.g. the Zope doctest module. [malthe]

  • Added tests for meta-directives and fixed some minor errors. [malthe]

  • Added update-tool to walk a source tree and automatically rewrite template import statements in each file. [malthe]

  • Added meta-directives for browser pages and viewlets. These build upon the original implementations, but make sure that the Chameleon template engine is used. [malthe]

  • Added PageTemplateFile. [malthe]

0.3 (2008-10-02)

  • Various changes.

0.2 (2008-09-13)

  • Various changes.

0.1 (2008-09-09)

  • Initial release.

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

z3c.ptcompat-0.5.6.tar.gz (13.7 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