Previous Chapter | Up | Next Section | Contents

Using document templates with ZPublisher


Document templates may be published directly with ZPublisher. ZPublisher treats document templates as if they were Python functions that accept the arguments named ' self ' and ' REQUEST '. The object traversed to get to the document template is passed as the ' self ' argument and the request object is passed as the ' REQUEST ' argument. Typically, document templates are defined as class attributes and passed class instances and request data, so instance and request data can be used to generate text.

Document templates may also be used in Python functions called by document templates, as in:

def getResults(self, key, REQUEST):
result_data=self.search(key)
return self.resultTemplate(selfdocument, REQUEST, search_results=result_data)

Be sure to call the document template. A common mistake is to return the document template directly, as in:

def getResults(self, key, REQUEST):
result_data=self.search(key)
return self.resultTemplate

ZPublisher does not attempt to call an object returned from a published object. Results of calling a published function are simply converted to strings and returned. When a document template is converted to a string, the document template source is returned. In the example above, the document template source, rather than the rendered template is returned.

Previous Chapter | Up | Next Section | Contents