ZPT Usage

This document focuses on usage of the Page Templates API.

For information about writing Page Templates documents and using TAL and TALES, refer to the reference documentation or the Chameleon documentation if you are using z3c.ptcompat.

Simple Usage

Using Page Templates is very easy and straight forward. Let’s look at a quick example. Suppose we have a file called hello_world.pt with these contents:

<html><body>Hello World</body></html>
>>> from zope.pagetemplate.pagetemplatefile import PageTemplateFile
>>> my_pt = PageTemplateFile('hello_world.pt')
>>> print(my_pt().strip())
<html><body>Hello World</body></html>

Subclassing PageTemplates

Lets say we want to alter page templates such that keyword arguments appear as top level items in the namespace. We can subclass PageTemplate and alter the default behavior of pt_getContext() to add them in:

from zope.pagetemplate.pagetemplate import PageTemplate

class mypt(PageTemplate):
    def pt_getContext(self, args=(), options={}, **kw):
       rval = PageTemplate.pt_getContext(self, args=args)
       return options

class foo(object):
    def getContents(self): return 'hi'

So now we can bind objects in a more arbitrary fashion, like the following:

template = """
<b tal:replace="das_object/getContents">Good Stuff Here</b>

pt = mypt()