[Welcome] [TitleIndex] [WordIndex

Problem

You want to use Zope Page Templates for rendering HTML under Quixote. (Perhaps you already have ZPT pages from an existing project, or perhaps you want to have an HTML designer implement the pages, in which case ZPT has an advantage over PTL.)

Solution

It's straightforward to write a Quixote handler that uses a module implementing ZPT. (SimpleTAL is one such implementation.)

import cStringIO
from simpletal import simpleTAL, simpleTALES

def _q_index (request):
    input = open('/www/docroot/templates/index.html', 'r')
    template = simpleTAL.compileHTMLTemplate(input)
    input.close()

    context = simpleTALES.Context()
    # Filling in the context is left up to you.
    context.addGlobal("title", "Sample title")

    # AFAIK, SimpleTAL always assumes output will be sent to a file,
    # so we need to use a StringIO instance here.
    output = cStringIO.StringIO()
    template.expand(context, output)
    return output.getvalue()

Getting a bit fancier, you can also subclass the quixote.util.StaticFile class to make it easy to publish an entire directory of ZPT templates.

import cStringIO
from simpletal import simpleTAL, simpleTALES
from quixote import util

class ZPTFile (util.StaticFile):
    def __call__ (self, request):
        # Read contents of ZPT template
        input = open(self.path, 'r')
        template = simpleTAL.compileHTMLTemplate(input)
        input.close()

        context = simpleTALES.Context()
        # Filling in the context is left up to you.
        context.addGlobal("title", "Sample title")

        # AFAIK, SimpleTAL always assumes output will be sent to a file,
        # so we need to use a StringIO instance here.
        output = cStringIO.StringIO()
        template.expand(context, output)
        return output.getvalue()

The __call__ method is responsible for reading the contents of files, so the above code overrides the method to treat files as a ZPT template. To publish an entire directory of templates, tell StaticDirectory to use this subclass:

_q_exports = ['templates']

templates = util.StaticDirectory('/www/docroot/templates', use_cache=False, file_class=ZPTFile)

Note that caching is turned off, because enabling it would cause the template's output to be cached forever, which is almost certainly not what you want. Templates will be recompiled every time they're accessed, a minor inefficiency; modify __call__ to cache the compiled versions if this matters to you.

Discussion

Filling in the context is probably the most difficult part, and I can't offer much advice about the right design for it. It will be most problematic for the StaticFile subclass, because the contents of the context will probably vary depending on the template being rendered. If you use ZPT in a Quixote application, please edit this recipe to record how you used it; your experience would be valuable.

The QuixoteAndSimpleTal page contains two helper functions that let you format templates using a simple dictionary for the context.

Also see QuixoteAndSimpleTalAdvanced for more complete solution.


CategoryCookbook CategoryQuixote1


2010-09-22 22:14