CherryPy with Zope Page Templates (ZPT)
Zope Page Templates are developed as part of Zope3, but can be used outside of Zope with very little effort.
- Check out the zpkgtools from Zope3 Subversion; this will be used to build ZPT from Subversion:
svn co svn://svn.zope.org/repos/main/zpkgtools/trunk
- Generate a build of ZPT.
% cd trunk % ./bin/zpkg --resource-map=svn://svn.zope.org/repos/main/ReleaseSupport/trunk/PackageMaps/zope3.map -ca ZPTThis will produce ZPT-0.0.0.tgz. - Install the generated package:
% tar zxvf ZPT-0.0.0.tgz % cd ZPT-0.0.0 % make install - Now that ZPT has been installed, a simple sub-class will allow you to easily pass values into templates.
from zope.pagetemplate.pagetemplatefile import PageTemplateFile class simpleZpt(PageTemplateFile): def pt_getContext(self, args=(), options={}, **kw): rval = PageTemplateFile.pt_getContext(self, args=args) options.update(rval) return options
- To use a page template in CherryPy, simply return the result of calling the template from your method. For example:
@cherrypy.expose def page(): template = simpleZpt('pagetemplate.pt') return template()
- To pass top-level objects into a template, simply specify them as keyword arguments in the call. For example:
@cherrypy.expose def page(): template = simpleZpt('pagetemplate.pt') context={'user':'Anonymous', 'title':'Page Title'} return template(context=context)
For details on ZPT syntax, see the ZPT Reference.
You may want to try SimpleTAL instead - it has similar to ZPT functionality.
Older versions
| replace this | with this | |
| 2.0 | import cherrypy | from cherrypy import cpg as cherrypy |

