CherryPy Project Download

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.

  1. 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
    
  2. Generate a build of ZPT.
        % cd trunk
        % ./bin/zpkg --resource-map=svn://svn.zope.org/repos/main/ReleaseSupport/trunk/PackageMaps/zope3.map -ca ZPT
    
    This will produce ZPT-0.0.0.tgz.
  3. Install the generated package:
        % tar zxvf ZPT-0.0.0.tgz
        % cd ZPT-0.0.0
        % make install
    
  4. 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
    
    
  5. 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()
    
  6. 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.0import cherrypyfrom cherrypy import cpg as cherrypy

Hosted by WebFaction

Log in as guest/cherrypy to create/edit wiki pages