blob: ddfa1d35c385452c6c1e1612a37efc815d98bd71 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
===========
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
<https://pagetemplates.readthedocs.io/en/latest/>`_ or the
`Chameleon documentation
<https://chameleon.readthedocs.io/en/latest/reference.html>`_ if you
are using z3c.ptcompat.
.. testsetup::
__file__ = 'docs/using.rst'
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:
.. literalinclude:: hello_world.pt
.. doctest::
>>> 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
:class:`~.PageTemplate` and alter the default behavior of
:meth:`~.pt_getContext()` to add them in:
.. testcode::
from zope.pagetemplate.pagetemplate import PageTemplate
class mypt(PageTemplate):
def pt_getContext(self, args=(), options={}, **kw):
rval = PageTemplate.pt_getContext(self, args=args)
options.update(rval)
return options
class foo(object):
def getContents(self): return 'hi'
So now we can bind objects in a more arbitrary fashion, like the
following:
.. testcode::
template = """
<html>
<body>
<b tal:replace="das_object/getContents">Good Stuff Here</b>
</body>
</html>
"""
pt = mypt()
pt.write(template)
print(pt(das_object=foo()).strip())
.. testoutput::
<html>
<body>
hi
</body>
</html>
|