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/src/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/src/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/src/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/src/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/src/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/src/z3c/ptcompat/tests/test.pt>>>> issubclass(factory, Viewlet) True
Changelog
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.