Personal tools
You are here: Home Documentation Tutorials Grok Poller Tutorial Zope Page Templates

Zope Page Templates

How to create display templates using the templating language TAL.
This tutorial shows how to implement a simple polling application using Grok.
Page 6 of 14.

Now we have a poll in the database for testing, and we can see it in our view after we implement it. As its template language Grok uses Template Attribute Language (TAL), also referred to as Zope Page Templates (ZPT). It's an XML based language, but we won't be diving too deep in it in this tutorial.

Open app_templates/index.pt and write the following in it:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:tal="http://xml.zope.org/namespaces/tal">
  <body>
    <h1>Polls</h1>

    <ul>
      <li tal:repeat="poll context/values">
        <a href="" tal:attributes="href python: view.url(poll)"
                   tal:content="poll/question">
          A poll
        </a>
      </li>
    </ul>

    <a href="" tal:attributes="href python: view.url('add')">Add a poll</a>
  </body>
</html>

The code is kept simple in this example; in real life the XHTML code would use CSS styling etc. As you can see the template language uses XML attributes in the namespace tal to dynamically modify the elements they are placed in. The list items are repeated by using tal:repeat attribute, or more specifically with repeat TAL command. The repeat command equals to Python's for-loop, where the loop contents is the li element:

# self is a view instance
for poll in self.context.values:
    ...

context/values part is a TAL Expression Syntax (TALES) [1] expression, which is an expression system used in TAL. Grok defines the following special names for the templates [2]:

[1]TALES Web site <http://wiki.zope.org/ZPT/TALES>
[2]Taken from Grok overview Web site <http://grok.zope.org/doc/current/grok_overview.html#templates>
view
The view that this template is associated with
context
The model that is being viewed
request
The current request object
static
To make URLs to static content made available by this module

thus context/values calls values() method of the model being viewed. The foo/bar expressions are called path expressions, which allows you to access attributes and methods of objects. Methods are called automatically, so you don't use parenthesis for them, as context/values shows.

Tip

If you want to make sure that no methods are called, you can use nocall: modifier before the path: tal:repeat="item nocall:context/itemsvar"

The next TAL command in the template is attributes, which is used to replace attribute contents, in this case, it replaces the contents of href with the result of python: view.url(poll) expression. TALES expressions that is prefixed with python: runs the following snippet as Python. The Python environment works in the same namespace as TAL, so we can use view name in the Python code to access the view instance. Grok views has url() method [3] which is used to return an absolute URL to that exact view for the current context. The first attributes command returns an URL to the current Poll object in the loop, and the second returns a specific view called "add", which we haven't implemented yet; hence is the topic of the next section.

[3]Grok view reference <http://grok.zope.org/doc/current/reference/components.html#views>

content command replaces the content of the element with the TALES expression, so in this case "A poll" text will be replaced with poll/question or as in Python: poll.question.

Now you should be able to view all polls at http://localhost:8080/poller/, or at least the one we created in the debug console.