Zope Page Templates

Discover how ZPT attributes can work with HTML to create useful and compatible dynamic web sites.
Some Simple Examples

Now that we've discussed the theory behind ZPT, let's look at some examples. The first example is the skeleton document that Zope creates when we start a new page template. To do this, go to the /manage URL on your Zope server, and choose page template from the Add product list in the upper right-hand corner. (If you don't see page template on the menu, it could be that ZPT is not an installed product. You may have to upgrade your copy of Zope to a more modern version in order to use ZPT.) As is usual in the Zope world, you will then be asked to enter an ID (i.e., a unique character string that will appear in the URL) for this new product. Enter a short name and click “Add and edit”.

When you follow these instructions for a DTML document or method, you are brought to an editing screen that contains skeleton DTML. The same is true for a page template, except that the skeleton document obviously contains TAL and TALES expressions:

     <title tal:content="template/title">
      The title</title>
     <h2><span tal:replace="here/title_or_id">
          content title or id</span>
         <span tal:condition="template/title"
               optional template id</span></h2>
     This is Page Template
     <em tal:content="template/id">template id</em>.

This document might be short, but it effectively displays TAL and TALES and how they might be used in actual documents. Here are the TAL attributes that this skeleton uses:

  • The tal:content attribute replaces the contents of the tag with the value of the TALES expression. So in this example document, the words “The title” will be replaced by the value of the TALES expression template/title, which we will discuss below. The <title> and </title> tags, along with their attributes, are left alone, but the text between these tags is changed when the template is presented by Zope.

  • The tal:replace attribute is similar to tal:content, except that it replaces the content and the surrounding tags. This is frequently used with the <span> tag, which is largely used as a placeholder for such markup in any event. Thus everything from the first <span> tag to the first </span> tag, including the tags themselves, will be replaced by the value of the TALES expression here/title_or_id.

  • The tal:condition attribute only displays its contents if its value is a true value, which is anything other than 0, the empty string, an empty list or the built-in ZPT “nothing” variable.

You might notice that the second <span> tag contains two TAL attributes, tal:condition and tal:replace. When Zope encounters multiple TAL attributes in a given tag, it first evaluates definitions, then conditions, then repeat loops, then content and replace tags. (You cannot have both content and replace attributes in the same tag, because they are mutually exclusive.) In the second <span> tag, Zope inserts the optional template ID only if the template/title TALES expression is true.

In addition to content, replace and condition, TAL defines three other attributes:

  • tal:repeat allows us to loop over a list of items. If your template will display a list of search results, rows from a database or files in a folder, then you can loop over the contents and display the contents in a variety of ways.

  • tal:attributes allows us to replace (or add to) an attribute of the enclosing tag. For example, we can set the href attribute of an anchor (<a>) tag at runtime by adding a tal:attributes attribute in that <a> tag. The value of the TALES expression will be used to set that attribute when it is evaluated at runtime.

  • tal:define allows you to set a new variable, which is then accessible from within enclosed tags. This variable value can be passed either along to other TAL tags or displayed.


Now that we have covered TAL, let's look at the TALES expressions that we can assign to a TAL attribute.

The simplest sort of TALES expression is a “path expression”, which describes a Zope object relative to the template, its container or the request. Path expressions are similar to URLs, except that they begin with a name rather than the root object /. The final element of the path expression typically will be a property that can be displayed (using tal:content), tested (using tal:condition) or iterated over (using tal:repeat).

The ZPT skeleton document uses four TALES path expressions:

  • here/title_or_id, which returns the value of the optional “title” property (if it exists) or the mandatory “id” property (if there is no title) to a tal:replace attribute.

  • template/title, which returns the (optional) title of the current template to tal:condition followed by tal:replace.

  • template/id, which returns the “id” property of the current template.

A TALES path expression that begins with “template” refers to the template object itself, while one that begins with “here” refers to the object to which the template is being applied—it may be the template itself or another object entirely. Some other relative markers are “request” (the Zope HTTP request object), “repeat” (information about current tal:repeat loops), “options” (for the parameters passed to the template) and “container” (for the folder of the current object).

TALES path expressions are great but are not as flexible as we might sometimes need. TALES thus allows you to return Python code by beginning your TALES expression with “python:”. For example, we can include the result of a simple calculation in a template with the following code:

<p>2 + 2 = <span tal:replace="python:2+2">

There is also a “string:” TALES prefix that indicates that the value should be treated as a text string. String expressions may contain interpolated variable values by prefixing the variable name with $, as in Perl and shell scripts. You may optionally surround the variable name with curly braces ({ and }), as in ${x}.


Free Dummies Books
Continuous Engineering


  • What continuous engineering is
  • How to continuously improve complex product designs
  • How to anticipate and respond to markets and clients
  • How to get the most out of your engineering resources

Get your free book now

Sponsored by IBM

Free Dummies Books
Service Virtualization

Learn to:

  • Define service virtualization
  • Select the most beneficial services to virtualize
  • Improve your traditional approach to testing
  • Deliver higher-quality software faster

Get your free book now

Sponsored by IBM