ZPT Basics (part 1) - Putting It All In Context (
Page 5 of 5 )
You'll remember that I told you,
a while back, that every TALES path expression must begin with a variable name.
The TALES specification defines a number of such variables - here's a list of
the most important ones:
"nothing" - a null value
"repeat" - a built-in TALES variable used for repetition
"root" - the starting point for the object hierarchy
"here" - the current context
"template" - the template itself
"container" - the template's container
"request" - the request object
You've already seen the "template" context in action - now let's take a look
at the "request" context, which contains information about the current HTTP request.
Consider the following example,
You are currently viewing the URL <i tal:content="request/URL">URL comes
here</i>.
which, when rendered, displays the current URL:
You are currently viewing the URL
http://medusa:8080/ZPT%20Basics/MyFirstTemplate.
This context also comes in handy when you're building Web forms and need to access
the values of the various form variables. Consider the following simple DTML Document:
Please enter the following details:
<br>
<form action="formHandler" method="POST">
<p>
Your
name
<br>
<input type="text" size="20" name="name">
<br>
</p>
<p>
Your
favourite sandwich filling
<br>
<input type="text" size="15" name="filling">
<br>
</p>
<input type="submit" name="submit" value="Hit Me"><br>
</form>
This displays the following form to the user.
Next, create the form processing script, "formHandler", using a Page Template:
Hello, <span tal:replace="request/form/name">name here</span>.
I like
<span tal:replace="request/form/filling">filling type here</span>
sandwiches
too. Maybe we should exchange recipes.
Try it out - here's an example of what you might see:
The beauty of this example lies in its simplicity. I've used the "request" context
and obtained access to the form variables simply by specifying the appropriate
path - the "request/form/name" path for the form variable "name", and the "request/form/filling"
path for the form variable "filling".
And that's about it for this first part of the series. In this article, I gave
you a gentle introduction to the ZPT way of doing things, explaining the special
"content" and "replace" TAL attributes. In the next segment, I'll be looking at
a few more of TAL's special attributes, including the ones that let you define
variables and write conditional statements. Make sure you tune in for that one...and
until then, stay healthy!
Note: All examples in this article have been tested on Linux/i586 with Zope 2.5.0.
Examples are illustrative only, and are not meant for a production environment.
Melonfire provides no warranties or support for the source code described in this
article. YMMV!