You are not logged in Log in Join
You are here: Home » Zope Documentation » Books » The Zope Book Releases » The Zope Book (2.5 Edition) » Using Zope Page Templates

Log in
Name

Password

 
Previous Page Up one Level Next Page Using Zope Page Templates Comments On/Off Table of Contents

Chapter 5. Using Zope Page Templates

Page Templates are a web page generation tool. They help programmers and designers collaborate in producing dynamic web pages for Zope web applications. Designers can use them to maintain pages without having to abandon their tools, while preserving the work required to embed those pages in an application. In this chapter, you'll learn the basics about Page Templates including how you can use them in your web site to create dynamic web pages easily. In Chapter 9, "Advanced Page Templates", you'll learn about advanced Page Template features.

Anonymous User - July 3, 2002 12:57 pm:
Though the title is "Chapter 5", and though Chapter 9 makes frequent reference to Chapter 5, this document is
 in the TOC as chapter six, viz. filename: zpt.stx
 hfx_ben
Anonymous User - Dec. 3, 2002 12:10 pm:
For a version without comments: Click the "Com Off" button on the right side of the navigation bar at the top
 or bottom of the screen.
 -Andrew

The goal of Page Templates is to allow designers and programmers to work together easily. A designer can use a WYSIWYG HTML editor to create a template, then a programmer can edit it to make it part of an application. If required, the designer can load the template back into his editor and make further changes to its structure and appearance. By taking reasonable steps to preserve the changes made by the programmer, the designer will not disrupt the application.

Page Templates aim at this goal by adopting three principles:

  1. Play nicely with editing tools.
  2. What you see is very similar to what you get.
  3. Keep code out of templates, except for structural logic.

A Page Template is like a model of the pages that it will generate. In particular, it is a valid HTML page.

Anonymous User - Dec. 28, 2002 2:55 pm:
 1

Zope Page Templates versus DTML

Zope already has DTML, why do you need another template language. First of all, DTML is not aimed at HTML designers. Once a page has been converted into a template, it is invalid HTML, making it difficult to work with outside of the application. Secondly, DTML suffers from a failure to separate presentation, logic, and content (data). This decreases the scalability of content management and website development efforts that use these systems.

gregholmes - Apr. 19, 2002 3:22 pm:
 "Zope already has DTML, why do you need another template language." should probably have a question mark at
 the end.
 Alternately:

 "Why do we need another template language?  Zope already has DTML!"
Anonymous User - May 1, 2002 11:59 am:
 Probably in the third sentence "Once a page has been converted into a template..." the intention is to say
 something along the lines "When DTML is used in a page..." or similar.
 The use of the word "template" looks like a real mistake to me. I also object to using "convert" because it
 sounds like an automated (and thus repeatable) step and thus the reader might get a wrong impresson. The
 latter may be very subjective, though.
Anonymous User - May 23, 2002 11:31 pm:
 I agree that converted is a poor choice, until I read farther in, I assumed that this was a part of the
 import process. A better phrasing might be "Once a page has been rewritten as DTML..."
Anonymous User - Sep. 10, 2002 11:09 am:
 Furthermore, since this chapter is entitled "Using Zope Page Templates", I think it's important that you
 specify
   "Once a page has been [converted] into a *DTML* template..."

 (I assume that is what is meant.)

DTML can do things that Page Templates can't such as dynamically generate email messages (Page Templates can only generate HTML and XML). So DTML won't go away. However, we do see Page Templates taking over almost all HTML/XML presentation work in Zope.

How Page Templates Work

Page Templates use the Template Attribute Language (TAL). TAL consists of special tag attributes. For example, a dynamic page title might look like this:


<title tal:content="here/title">Page Title</title>
Anonymous User - Sep. 10, 2002 11:15 am:
 This info regarding TAL should have accompanied or preceded the part of Chapter 3 "Coding Logic with
 Scripts", where I first came across it in some example code and had no idea what it was.
Anonymous User - Oct. 4, 2002 9:26 am:
 Where is the keyword "here" defined? Is this just defined for tal expressions only? What does it mean? Why
 don't any of the docs explain it?
Anonymous User - Dec. 27, 2002 4:06 pm:
 See Appendix D. here means the object on which the page template will be applied. I know it is kind of
 frustrating to read the Appendix first and then the basic chapter.
Anonymous User - Jan. 25, 2003 11:01 am:
 You should really scrap DTML switch to XSLT. Much more powerful, and a standard to boot.

The tal:content attribute is a TAL statement. Since it has an XML namespace (the tal: part) most editing tools will not complain that they don't understand it, and will not remove it. It will not change the structure or appearance of the template when loaded into a WYSIWYG editor or a web browser. The name content indicates that it will set the content of the title tag, and the value "here/title" is an expression providing the text to insert into the tag.

Anonymous User - Dec. 26, 2002 11:27 pm:
 I tried to Test the page but nothing showed up. I was expecting that atleast the Title of the page will be
 the title of my ZPT or if is is blank then the "Page Title". No explaination is given.

All TAL statements consist of tag attributes whose name starts with tal: and all TAL statements have values associated with them. The value of a TAL statement is shown inside quotes. See Appendix C, "Zope Page Templates Reference", for more information on TAL.

Anonymous User - Dec. 19, 2002 8:52 pm:
 Why not tell us about TAL right now? This chapter is about using templates. What is the problem with telling
me right now? After all, this chapter should be discussing how to use TAL and page templates. So why not tell
 me? This book reads like one big pop-up ad. Where's the actual info?

To the HTML designer using a WYSIWYG tool, the dynamic title example is perfectly valid HTML, and shows up in their editor looking like a title should look like. In other words, Page Templates play nicely with editing tools.

This example also demonstrates the principle that, "What you see is very similar to what you get". When you view the template in an editor, the title text will act as a placeholder for the dynamic title text. The template provides an example of how generated documents will look.

When this template is saved in Zope and viewed by a user, Zope turns the dummy content into dynamic content, replacing "Page Title" with whatever "here/title" resolves to. In this case, "here/title" resolves to the title of the object to which the template is applied. This substitution is done dynamically, when the template is viewed.

Anonymous User - Aug. 6, 2002 12:12 am:
What is the object to which the template is applied? In reading the rest of the chapter it seems like at this
 time the only objects to which the template can be applied is the folder which is the "container" object
 which is accessible to scripts.
 If you can apply templates to other objects how do you apply them to other objects? There seems to be only
 one way to apply an object to a template and this is implicit. You invoke the url to the template or
 <dtml-var> it and the template automatically runs against the container.
How do you call a template and apply it to another obejct? The Advanced chapter says you can call a template,
 but how do you do this. I can't find the on-line documentation for this.
Anonymous User - Sep. 21, 2002 10:43 pm:
 Agree. What do you mean "object to which the template is applied"?
 When and how is a template *applied* to an object?
 Or do you mean it is published in the context of an object?
 Then "here" would be the same as "context" for PythonScripts (see chap 10).
 How abt losing some words abt the general publishing process of zope?
 Also:
 /when the template is viewed/when the template is published/
Anonymous User - Dec. 27, 2002 12:26 am:
 This is one of lousy documentation that I have even seen.
Anonymous User - Dec. 28, 2002 1:13 am:
 And without doubt it would have been much better had *you* written it.  Well, funnier maybe.

There are template statements for replacing entire tags, their contents, or just some of their attributes. You can repeat a tag several times or omit it entirely. You can join parts of several templates together, and specify simple error handling. All of these capabilities are used to generate document structures. Despite these capabilities, you can't create subroutines or classes, perform complex flow control, or easily express complex algorithms. For these tasks, you should use Python-based Scripts or application components.

Anonymous User - May 22, 2002 6:56 pm:
 Please give examples of "subroutines or classes, perform complex flow control, or easily express complex
 algorithms"
Anonymous User - Sep. 10, 2002 11:33 am:
 If you really want to emphasize "can't", you should spell out "cannot".

 Your friends, Strunk & White
Anonymous User - Dec. 27, 2002 12:29 am:
 Please provide examples to support your statements.

The Page Template language is deliberately not as powerful and general-purpose as it could be. It is meant to be used inside of a framework (such as Zope) in which other objects handle business logic and tasks unrelated to page layout.

For instance, template language would be useful for rendering an invoice page, generating one row for each line item, and inserting the description, quantity, price, and so on into the text for each row. It would not be used to create the invoice record in a database or to interact with a credit card processing facility.

Anonymous User - June 4, 2002 8:04 am:
 How about dynamically creating the options in a popup (select) form?
Anonymous User - Sep. 10, 2002 11:37 am:
 Please comment here on where <dtml-in> fits into this scheme.

Creating a Page Template

If you design pages, you will probably use FTP or WebDAV instead of the Zope Management Interface (ZMI) to create and edit Page Templates. See the "Using FTP and WebDAV" section later in this chapter for information on editing Page Templates remotely. For the small examples in this chapter, it is easier to use the ZMI.

BubbaFett - May 23, 2002 11:57 am:
 You cannot "create...Page Templates" with FTP and WebDAV.  This is explained below, but it's unclear here.

Use your web browser to log into the Zope Management Interface as a manager. Choose a Folder to work in (the root is fine) and pick "Page Template" from the drop-down add list. Type "simple_page" in the add form's Id field, then push the "Add and Edit" button.

You should now see the main editing page for the new Page Template. The title is blank, the content-type is text/html, and the default template text is in the editing area.

Now let's create a simple dynamic page. Type the words "a Simple Page" in the Title field. Then, edit the template text to look like this:


<html>
 <body>
   <p>
     This is <b tal:replace="template/title">the Title</b>.
   </p>
 </body>
</html>

Now push the Save Changes button. Zope should show a message confirming that your changes have been saved.

Anonymous User - May 24, 2002 8:55 am:
 all I get is "This is <b>the Title.</B> " I am using Zope 1.3 beta  if that matters.
Anonymous User - May 27, 2002 5:49 am:
 Did you really type "a Simple Page" in the Title field
 when you created the template???
Anonymous User - June 26, 2002 4:54 am:
 Version mismatch? I thought that ZPT was a relatively new feature, certainly not going back to 1.3b.
Anonymous User - June 26, 2002 9:49 am:
 That's right: Page Templates were introduced in version 2.5:
 http://www.zope.org/Products/Zope/2.5.0/zope_250_release
Anonymous User - Sep. 21, 2002 10:51 pm:
 Before 2.5, they were a separate product; thus you may have <2.5 w zpts. but 1.3 seems pretty dusted.
Anonymous User - Dec. 3, 2002 12:08 pm:
When I tried this, I thought "field" meant the space between <title> and </title>. Then I
understood it meant
 the field named "title" of the object. Then it worked.

If an HTML comment starting with <-- Page Template Diagnostics is added to the template text, then check to make sure you typed the example correctly and save it again. This comment is an error message telling you that something is wrong. You don't need to erase the error comment; once the error is corrected it will go away.

Anonymous User - May 6, 2002 7:51 am:
 Does not an HTML comment start with <!--  ?
Anonymous User - May 20, 2002 8:10 pm:
 This is.      is the only thing I get
Anonymous User - May 24, 2002 9:02 am:
 dtml document and dtml method view with same result. "This is the title". The "tal:replace" must not be not
 working.
Anonymous User - May 24, 2002 3:23 pm:
 Oh, I figured out what the problem is. DONT USE DTML METHOD OR DOCUMENT. You MUST use "Page Template" in CMF
 1.3 it does not work on dtml method or document.
Anonymous User - May 27, 2002 5:51 am:
 Of course it doesn't. Why should it?
Anonymous User - May 29, 2002 10:38 am:
 since the instructions earlier were unclear as to how the example was made, it was unclear how exactly to
 make the original page template. All that was needed to say was to use CMF "Page Template" from the "Select
 type to add..." menu. Then there would be no confusion.
Anonymous User - July 9, 2002 10:05 am:
 did you add "CMF Page Template" or just "Page Template"? 'cause there is no "CMF Page Template" in the
 selection. and if you add just a "Page Template" you can either view nor edit it in your CMF site...
Anonymous User - July 29, 2002 4:52 am:
 "<-- Page Template Diagnostics "

 Comments don't start with "<--". They start with "<!--".

Click on the Test tab. You should see a page with, "This is a Simple Page." at the top. Notice that the text is plain; nothing is in bold.

Anonymous User - May 24, 2002 8:57 am:
 there is no "Test" tab, do you mean "View"?
Anonymous User - May 29, 2002 10:39 am:
 "Test" tab only shows when using "Page Template", "View" only shows when using DTML page or DTML method.
Anonymous User - July 30, 2002 1:23 pm:
 * I don't think it can be "a" in "This is a Simple Page"  
   because there is no "a" in :
   This is <b tal:replace="template/title">the Title</b>.
 * I only get "This is .", unless I change the line to:
   This is <b tal:replace="template/id">the Title</b>.
 Did I miss something?
Anonymous User - July 30, 2002 1:38 pm:
 Sorry - I left Title field blank :(
Anonymous User - Dec. 27, 2002 12:48 am:
 How this example is different from the first example. Not enough explaination is provided in both the
 examples.
Anonymous User - Dec. 27, 2002 12:54 am:
 Why don't I get "a Simple Page" in Bold.
Anonymous User - Dec. 27, 2002 4:38 pm:
 because <b> tag has been replaced by the title of the page.

Back up, then click on the Browse HTML source link under the content-type field. This will show you the unrendered source of the template. You should see, "This is the Title." Back up again, so that you are ready to edit the example further.

Anonymous User - Apr. 21, 2002 10:17 am:
 I don't see 'the Title' in bold - I just see the HTML source.
Anonymous User - Apr. 27, 2002 8:49 pm:
 This example needs to use 'content' instead of 'replace' as the bold tag is getting replaced out of the
 result.
Anonymous User - May 24, 2002 9:20 am:
I see no "Browse HTML source link under the content-type field. " Which version are you using? I am using CMF
 1.3 under Zope 2.5.1
Anonymous User - Sep. 10, 2002 2:52 pm:
 Back up?  Where?  In this book?

The Content-Type field allows you to specify the content type of your page. Generally you'll use a content type of text/html HTML or text/xml for XML.

Anonymous User - July 3, 2002 1:19 pm:
 How do I change the example to work with another content type, i.e. as well-formed XML? All examples that
 follow in this chapter don't work properly with text/html.

If you set the content-type to text/html then Zope parses your template using HTML compatiblity mode which allows HTML's loose markup. If you set your content-type to something other than text/html then Zope assumes that your template is well formed XML. Zope also requires an explicit TAL and METAL XML namespace declarations for well formed XML.

viehmann - May 1, 2002 12:22 pm:
 Maybe new users want to start out properly right away. I'd really like to see a well formed XML example
 either here or even have the above example in well formed XML.
Anonymous User - Sep. 22, 2002 4:19 am:
 /requires an explicit/requires explicit/
Anonymous User - Sep. 22, 2002 4:21 am:
 /Zope also/In the latter case Zope also/
Anonymous User - Dec. 3, 2002 12:15 pm:
 > explicit TAL and METAL XML namespace declarations

Please, just give the declarations! ( &&*$%#$$-ing namespaces... All they do is cause trouble. TAL
works
 because it is like architectural forms!)
Anonymous User - Dec. 27, 2002 4:45 pm:
 It is really funny. Before reading this chapter, one has to read the Appendix D first. I thought Appendix
 chapters are more like reference chapters.
Anonymous User - Dec. 27, 2002 4:47 pm:
 It is really funny. Before reading this chapter, one has to read the Appendix D first. I thought Appendix
 chapters are more like reference chapters.

XXXAmos- refer to a place where you give an example of above -TD

The Expand macros with editing control is explain in Chapter 9, "Advanced Page Templates".

gregholmes - Apr. 19, 2002 3:27 pm:
 The Expand macros with editing control is explain in Chapter 9, "Advanced Page Templates".

 Please replace "explain" with "explained".
Anonymous User - Aug. 6, 2002 10:15 pm:
 And "Expand macros with editing" should be "Expand macros when editing"

Simple Expressions

The expression, "template/title" in your simple Page Template is a path expression. This the most common type of expression. There are several other types of expressions defined by the TAL Expression Syntax (TALES) standard. For more information on TALES see Appendix C, "Zope Page Templates Reference".

viehmann - May 1, 2002 12:29 pm:
In my understanding, TALES is specific to Zope. (Disregard this comment if it's not.) If it is, calling TALES
 a "standard" is probably a bit too enthusiastic. (Although the availability of a decent specification is
 certainly a very important feature that is worth mentioning here.)
Anonymous User - July 10, 2002 8:06 am:
 "Proposed standard" might be more appropriate.
Anonymous User - Aug. 30, 2002 12:53 pm:
 "specification", perhaps?
Anonymous User - Sep. 25, 2002 4:52 pm:
 BLF: Section Heading "Simple Expressions", Section Body "path expressions" ?

The "template/title" path expression fetches the

title

property of the template. Here are some other common path expressions:

  • 'request/URL': The URL of the current web request.
  • 'user/getUserName': The authenticated user's login name.
  • 'container/objectIds': A list of Ids of the objects in the same Folder as the template.

Anonymous User - Dec. 5, 2002 1:34 am:
 yuiuyiyuiytuiuyrui

Every path starts with a variable name. If the variable contains the value you want, you stop there. Otherwise, you add a slash (/) and the name of a sub-object or property. You may need to work your way through several sub-objects to get to the value you're looking for.

Zope defines a small set of built-in variables such as request and user, which are described in Chapter 9, "Advanced Page Templates". You will also learn how to define your own variables in that chapter.

XXXAmos: Perhaps a new Appendix could be added, or added to Appendix C, where the objects/subobjects tree is shown-TD

Inserting Text

In your "simple_page" template, you used the tal:replace statement on a bold tag. When you tested it, Zope replaced the entire tag with the title of the template. When you browsed the source, you saw the template text in bold. We used a bold tag in order to highlight the difference.

Anonymous User - May 6, 2002 8:12 am:
 Wasn't it <b tal:content="....">...?
Anonymous User - Sep. 10, 2002 2:56 pm:
 When I browsed which source? The HTML source, or the Page Template source? This tutorial has several spots
 where a reader might get lost or confused.
Anonymous User - Dec. 27, 2002 4:49 pm:
 Just Click on the "Browse HTML Source" in Page Template Edit view.

In order to place dynamic text inside of other text, you typically use tal:replace on a span tag rather than on a bold tag. For example, add the following lines to your example:


<br>
The URL is <span tal:replace="request/URL">URL</span>.
kaleissin - May 16, 2002 2:49 pm:
 Better to replace the final 'URL' with something like 'http://www.example.com/' or 'this is replaced by a
 computed url' or similar, since this part is not magical and ought to serve as a reminder of what the final
 result will be.
Anonymous User - Sep. 22, 2002 5:31 am:
 /place dynamic text/place text dynamically/
 or leave the dynamo out, since "to place" already denotes an action in time.
 there is no "dynamic text", there is "dynamic placement"

The span tag is structural, not visual, so this looks like "The URL is URL." when you view the source in an editor or browser. When you view the rendered version, it may look something like:


<br>
The URL is http://localhost:8080/simple_page.
Anonymous User - Sep. 10, 2002 2:59 pm:
 Again, which source? Viewing the source in a browser leads me to believe you mean the Page Template source,
 but I think you really mean the HTML source, but I'm losing sense of the difference here.
Anonymous User - Nov. 15, 2002 5:07 am:
 In test-view the br-tag wil not be shown.

If you want to insert text into a tag but leave the tag itself alone, you use the tal:content statement. To set the title of your example page to the template's title property, add the following lines between the html and the body tags:


<head>
  <title tal:content="template/title">The Title</title>
</head>

If you open the "Test" tab in a new browser window, the window's title will be "a Simple Page". If you view the source of the page you'll see something like this:


<html>
  <head>
    <title>a Simple Page</title>
  </head>
...

Zope inserted the title of your template into the title tag.

Anonymous User - Sep. 10, 2002 2:49 pm:
For the point you're making here, you should be more precise than "into the title tag", because that could be
confused with what happened with the bold tag example. Since the difference is the crux of what you're saying
here, maybe "Zope inserted the title of your template between the start and end tags of the <title>
element."

Repeating Structures

Now let's add some context to your page, in the form of a list of the objects that are in the same Folder as the template. You will make a table that has a numbered row for each object, and columns for the id, meta-type, and title. Add these lines to the bottom of your example template:


<table border="1" width="100%">
  <tr>
    <th>Number</th>
    <th>Id</th>
    <th>Meta-Type</th>
    <th>Title</th>
  </tr>
  <tr tal:repeat="item container/objectValues">
    <td tal:content="repeat/item/number">#</td>
    <td tal:content="item/getId">Id</td>
    <td tal:content="item/meta_type">Meta-Type</td>
    <td tal:content="item/title">Title</td>
  </tr>
</table>
Anonymous User - May 30, 2002 3:27 pm:
 This won't work in the root folder as even the manager doesn't have default permissions for getting
 properties of certain root files.
Anonymous User - May 30, 2002 3:34 pm:
 The actual error message is:

 Error Type: Unauthorized 
  Error Value: You are not allowed to access title in this context

 More on this below...

The tal:repeat statement on the table row means "repeat this row for each item in my container's list of object values". The repeat statement puts the objects from the list into the item variable one at a time (this is called the repeat variable), and makes a copy of the row using that variable. The value of "item/getId" in each row is the Id of the object for that row, and likewise with "item/meta_type" and "item/title".

Anonymous User - Sep. 22, 2002 6:02 am:
 TAL processing replaces text with text. So we dont have table rows, we have TR tags in the source. For each
object in the list the whole text "<tr ... /tr>" is first repeated(copied), then processed (using the
current
 object in the list) yielding the target text.
Anonymous User - Sep. 22, 2002 6:17 am:
 The named repeat variable is only defined in the corresponding repeat tag.
 HTML block tags may nest. When both an outer and an inner tag repeat, different names for outer and inner
 repeat variables allow to discern between outer and inner repetition.

You can use any name you like for the repeat variable ("item" is only an example), as long as it starts with a letter and contains only letters, numbers, and underscores (_). The repeat variable is only defined in the repeat tag. If you try to use it above or below the tr tag you will get an error.

You can also use the repeat variable name to get information about the current repetition. By placing it after the built-in variable repeat in a path, you can access the repetition count from zero (index), from one (number), from "A" (Letter), and in several other ways. So, the expression repeat/item/number is 1 in the first row, 2 in the second row, and so on.

Since a tal:repeat loop can be placed inside of another, more than one can be active at the same time. This is why you must write repeat/item/number instead of just repeat/number. You must specify which loop your interested in by including the loop name.

Anonymous User - May 7, 2002 7:20 am:
 ... which loop you're interested in...
Anonymous User - Sep. 22, 2002 6:25 am:
 Ok, i *must* name the loops. For nested ones i *should* use different names.
 In name resolution, the first one found is taken.

Now view the page and notice how it lists all the objects in the same folder as the template. Try adding or deleting objects from the folder and notice how the page reflects these changes.

Conditional Elements

Using Page Templates you can dynamically query your environment and selectively insert text depending on conditions. For example, you could display special information in response to a cookie:


<p tal:condition="request/cookies/verbose | nothing">
  Here's the extra information you requested.
</p>
Anonymous User - Sep. 10, 2002 3:22 pm:
 At what point would we be better off in Python?
Anonymous User - Jan. 14, 2003 6:47 pm:
 what is the value of 'nothing' ?? Is that a special variable and what is its value ?
 What is the meaning of '|' is zopesque for an 'OR' or an 'XOR' ??
 how is <p tal:condition="request/cookies/verbose | nothing">
 different from <p tal:condition="request/cookies/verbose"> ??

This paragraph will be included in the output only if there is a verbose cookie set. The expression, request/cookies/verbose | nothing is true only when there is a cookie named verbose set. You'll learn more about this kind of expression in Chapter 9, "Advanced Page Templates".

Using the tal:condition statement you can check all kinds of conditions. A tal:condition statement does nothing if its expression has a true value, but removes the entire statement tag, including its contents, if the value is false. Zope considers the number zero, a blank string, an empty list, and the built-in variable nothing to be false values. Nearly every other value is true, including non-zero numbers, and strings with anything in them (even spaces!).

XXXAmos: mmm, in fact the tal:condition statement does something when its expression is True. It inserts the information between its tags into the page... -TD

Anonymous User - Apr. 27, 2002 9:08 pm:
 Lets phrase that as ... if true it includes the tag and its contents, if false it excludes it.
Anonymous User - Sep. 10, 2002 3:20 pm:
I'm lost now. It would be much easier to figure this example out if the content of the <p> element were
 something useful like "You have the verbose cookie set" instead the arbitrary "Here is the information you
 requested."

Another common use of conditions is to test a sequence to see if it is empty before looping over it. For example in the last section you saw how to draw a table by iterating over a collection of objects. Here's how to add a check to the page so that if the list of objects is empty no table is drawn:


<table tal:condition="container/objectValues" 
       border="1" width="100%">
  <tr>
    <th>Number</th>
    <th>Id</th>
    <th>Meta-Type</th>
    <th>Title</th>
  </tr>
  <tr tal:repeat="item container/objectValues">
    <td tal:content="repeat/item/number">#</td>
    <td tal:content="item/getId">Id</td>
    <td tal:content="item/meta_type">Meta-Type</td>
    <td tal:content="item/title">Title</td>
  </tr>
</table>
Anonymous User - Apr. 17, 2002 10:38 am:
 Sample does not completely work for me (in root "/" ).  
 I have to take out the last tal line (item/title) 
 otherwise I obtain authentification error! 

 Error Type: Unauthorized 
 Error Value: You are not allowed to access title in this context
Anonymous User - Apr. 27, 2002 9:16 pm:
 Works fine in all 'normal' folders
Anonymous User - May 23, 2002 1:47 pm:
 Same thing happens to me, but it happens in the earlier tal:repeat example, so I'd move this comment higher
 up.
 Also in Mozilla, hitting "cancel" actually logs me out so it's doubly inconvenient.
Anonymous User - May 30, 2002 3:39 pm:
 This is kind of a bad example of using a condition to not show anything.
 At the minimum it will show a table with one file (the file containing the code) and never not show the
 table.
Anonymous User - June 11, 2002 10:58 am:
 actually, I would like to see a way for the table to be created and exclude the page with the script. Ie.
 show everything in the containing folder OTHER than this page. (or how to EXCLUDE files in the report).
 Also, it would be nice to talk about how to add dynamic URLS to each file listing in the table.
Anonymous User - Jan. 15, 2003 3:02 pm:
 well, this one isn't smart at all... would be better to show a conditional 
 example similar to 
 if (something = "bla"){
   do something...
 }

If the expressions, container/objectValues is false then the entire table is omitted.

Anonymous User - Sep. 22, 2002 6:32 am:
 /expressions,/expression/

Changing Attributes

Most, if not all, of the objects listed by your template have an icon property, that contains the path to the icon for that kind of object. In order to show this icon in the meta-type column, you will need to insert this path into the src attribute of an img tag. Edit the meta-type column in both rows to look like this:


<td><img src="/misc_/OFSP/Folder_icon.gif"
         tal:attributes="src item/icon">
    <span tal:replace="item/meta_type">Meta-Type</span>
</td>

XXXAmos: There is only one row, to which above code is usefull.... shouldn't it be 'Edit the meta-type column in the celldata row to look like this'-TD

The tal:attributes statement replaces the src attribute of the img tag with the value of item/icon. The src="/misc_/OFSP/Folder_icon.gif" attribute in the template acts as a placeholder.

Anonymous User - July 4, 2002 1:05 pm:
 But it also works if you omit the placeholder, at least on my Zope 2.5.1:

   <td><img tal:attributes="src foo/icon">
       <span tal:replace="foo/meta_type">Meta-Type</span></td>

 So is the 'src=...' placeholder redundant?  The above is rather unclear.
Anonymous User - July 11, 2002 4:00 pm:
 It works, but if click "Browse HTML source" you get an unloaded image...
Anonymous User - Sep. 22, 2002 6:34 am:
 /replaces the src attribute/replaces the value of the src attribute/
Anonymous User - Sep. 22, 2002 6:44 am:
 shouldnt this be "src foo/icon.absolute_url" ? the path of, not the image.

Notice that we've replaced the tal:content attribute on the table cell with a tal:replace statement on a span tag. This change allows you to have both an image and text in the table cell.

Anonymous User - Aug. 19, 2002 11:36 am:
 What would happen if one didn't use the tal:replace statement on the span tag but still the tal:content
 statement?
 What kind of effect would you see or if you don't see any effect at all - why would it be so?
Anonymous User - Aug. 21, 2002 11:24 am:
You will see no difference when you see the result in a browser, but that's because the <span> tag has
no
 visual representation. It's used as a placeholder for attributes, styles and other things.
 Example. The following template:
    <p>Title: <span tal:replace="template/title">dummy title</span></p>
 will be processed as:
    <p>Title: The Real Title</p>

 But the following other template:
    <p>Title: <span tal:content="template/title">dummy title</span></p>
 will be processed as:
    <p>Title: <span>The Real Title<span></p>

 The visual presentation to the final user in their browser is the same.

Creating a File Library with Page Templates

Here's an example of using Page Templates with Zope to create a simple file library with one template, a little bit of Python code, and some files.

First, create a mock up of a file library page using your HTML editor. The examples in this chapter were made with Amaya. This mock-up doesn't need to overdo it, it just shows some dummy information. Here's a mock-up of a file library that contains one file:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>File Library</title>
  <style type="text/css">
  .header {
    font-weight: bold;
    font-family: helvetica;
    background: #DDDDDD;
  }
  h1 {
    font-family: helvetica;
  }
  .filename {
    font-family: courier
  }
  </style>
  <meta name="GENERATOR" content="amaya 5.1">
</head>

<body>
<h1>File Library</h1>

<p>Click on a file below to download it.</p>

<table border="1" cellpadding="5" cellspacing="0">
  <tbody>
    <tr>
      <td class="header">Name</td>
      <td class="header">Type</td>
      <td class="header">Size</td>
      <td class="header">Last Modified</td>
    </tr>
    <tr>
      <td><a href="Sample.tgz" class="filename">Sample.tgz</a></td>
      <td>application/x-gzip-compressed</td>
      <td>22 K</td>
      <td>2001/09/17</td>
    </tr>
  </tbody>
</table>
</body>
</html>

Now, log into your Zope and create a folder called FileLib. In this folder, create a Page Template called index_html by selecting Page Template from the add menu, specifying the Id index_html in the form, and clicking Add.

Now, with your HTML editor, save the above HTML to the URL of the index_html Page Template followed by /source.html, for example, http://localhost:8080/FileLib/index_html/source.html. Notice that the URL to save the index_html page ends in source.html. Because Page Templates are dynamic, you need a way to edit the raw source of the template, unrendered by the page template language. Appending source.html to a Page Template gives you this raw source. Note, if the content-type of your page is text/xml then you'll use source.xml, rather than source.html.

Anonymous User - Apr. 22, 2002 2:16 pm:
The index_html object is a file not a directory so saving to index_html/source.html doesn't make sense.?.? :\
Anonymous User - May 29, 2002 5:28 pm:
 ---Agreed. I find this paragraph totally baffling.
Anonymous User - June 2, 2002 3:05 am:
 Some HTML editors have a "save to URL" options. (In mozilla composer it is called "Publish As".) The idea is
 that you provide a URL on the server and the editor uses it to submit your file. true, index_html is not a
directory, but neither is "FileLib". Both are just objects in Zopes database and segments of the URL string.
Anyway,
 if you use one of the tools that have this feature, this paragraph should make sense to you. If you don't,
 don't worry about - just paste the HTML source into the Zope interface!
Anonymous User - June 11, 2002 11:14 am:
How about we include a list of "compatible" editors that CAN do this as well as a list of editors that CANNOT
 do this?
 it seems you are making assumptions that we all use the same tools you use.
mcdonc - June 15, 2002 11:45 am:
 Are you going to keep that list current for me? ;-)
Anonymous User - June 20, 2002 10:37 am:
 Does the name 'source.html' have the special meaning that Zope will use the contents as source, or would any
 'XXX.html' work?
Anonymous User - June 24, 2002 10:36 am:
 did i miss it or did we go over how zope stores files internally already. does the files all being in a
 database limit you to managing zope using the web interface?
Anonymous User - July 19, 2002 6:15 pm:
 I agree, very confusing paragraph. Are we HTTP saving/updating index_html (i.e. just saving the changes
 directly from maya into Zope using HTTP rather than using ZMI?). If so, it is very confusing to use NEW WAYS
 OF EDITING OBJECTS that have not been explained in a section meant to teach something else...
Anonymous User - Aug. 6, 2002 11:00 pm:
 This confused me also. A couple suggestions to make it hopefully more clear:
1. Tell us which protocol is being used with this "save to URL" function of your HTML editor. Is it HTTP PUT?
 Something else? Beats me.
 2. Give primary instructions in a non-specific manner (i.e., tell us to use the ZMI first). Then say
 something like "If your HTML editor supports [protocol], you can use it save to the URL of the index_html
 Page Template followed by /source.html.
Anonymous User - Aug. 13, 2002 2:52 pm:
 Hey  "mcdonc",
 at this point we'd like to know ANY editor that works. whether current or old versions!
Anonymous User - Aug. 27, 2002 7:54 pm:
This doesn't make any sense with the tools I'm using. In Dreamweaver, you can't upload a file to a file, only
 a folder. I don't know what the documentation is referring too. What I did discover is that in Dreamweaver,
 you can name the file the same name as your object and upload it and everything works fine. If the object
 name is "index_html" then just name your file "index_html" without the dot syntax, and upload the file.
Everything works fine. If you look at the object in zope, you will see that it is still a template object and
 behaves as one.
Anonymous User - Sep. 22, 2002 6:52 am:
 How abt editing the template source in ZMI and just copy/paste above code into the text area?
 Or is this too clever?
Anonymous User - Jan. 15, 2003 3:34 pm:
 welll, as crapy it may look and sound.. it works really.. BUT...a better way is to copy/paste the content
 into the page template file index_html i guess ;)

XXXAmos:Is it possible to save a Page Template to Zope without first creating a page template object? -TD

Anonymous User - Jan. 4, 2003 4:52 pm:
 I've just created a program template in FileLib, called index.html
 and filled it with the source above using the MI.
 No messing about with a FTP tool I have not, not trying to put a file
 into a file (which does not work) nor trying to create a directory 
 with the same name oas a file (not permitted). 
 It isn't a file type (so long as I don't up-load it with FTP), so it won't
 appear in the listing when I get that far. 
 It appears to be working (so far). 

 Seldom have so many owed so much confusion to so few!
Anonymous User - Jan. 4, 2003 5:22 pm:
 Having renamed it index_html it now  displays the completed table, 
 from the url ..../FileLib    !
Anonymous User - Jan. 4, 2003 5:39 pm:
 I don't quite understand the actual problem being described but I *had* to add the "seldom have so many owed
 so much confusion to so few!" quote to ZopeZen. This is classic, it made me laugh out loud. ;-) -chrism

Now that you've saved the template, you can go back to Zope and click on index_html and then click on its Test tab to view the template. It looks just like it the mock-up, so everything is going well.

Anonymous User - Sep. 22, 2002 6:53 am:
 /it //

Now let's tweak the above HTML and add some dynamic magic. First, we want the title of the template to be dynamic. In Zope, you'll notice that the Page Template has a title form field that you can fill in. Instead of being static HTML, we want Zope to dynamically insert the Page Templates title into the rendered version of the template. Here's how:


<head>
  ...
  <title tal:content="template/title">File Library</title>
  ...

<body>
<h1 tal:content="template/title">File Library</h1>
...
Anonymous User - Jan. 25, 2003 3:24 pm:
 "dynamically insert the Page Templates" should be "dynamically insert the Page Template's" (missing
 apostrophe)

Now go to Zope and change the title of the index_html page template. After saving that change, click the Test tab. As you can see, the Page Template dynamically inserted the title of the template object in the output of the template.

Notice the new content tag attribute in the tal xml namespace. This attribute says to "replace the content of this tag with the variable 'template/title'". In this case, template/title is the title of the index_html Page Template.

Anonymous User - Jan. 24, 2003 10:26 am:
 Rather than, "notice the new content tag attribute in the tal xml namespace...," how about:
"When you added the "tal:content" attribute to the <title> or <h1> tag, it instructed Zope to
replace the
 content of the tag with the variable, 'template/title'."

XXXAmos: This is the first time you speak about the tal xml namespace. It would be good to use/explain this terminology in the beginning of this chapter, where we start using the tal expression -TD

The next bit of magic is to build a dynamic file list that shows you all the File objects in the FileLib folder.

To start, you need to write just one line of Python. Go to the FileLib folder and create a Script (Python) in that folder. Give the script the id files and click Add and Edit. Edit the script to contain the following Python code:


## Script (Python) "files"
## 
return container.objectValues('File')
Anonymous User - May 19, 2002 12:37 pm:
I see a 'return' statement at the end of this script. Does it mean, that every zope script is body of 'def' ?
 (or body of 'sub', if perl script)
Anonymous User - May 19, 2002 1:49 pm:
 Yes, but it is implied by the script.  You shouldn't type it.
Anonymous User - May 23, 2002 11:43 am:
"def" means to create a function, which can then be called. In this example. the script just returns the list
 of files objects, it doesn't need to define a function.
Anonymous User - June 11, 2002 5:40 pm:
 script should be 
 return container.objectValues(['File'])
 so that you can add file types such as
 return container.objectValues(['Image', 'File'])
 Now you can get a report (return) for both files and images.
Anonymous User - July 1, 2002 6:32 am:
 I think it's more accurate to state that the return is there to return a value from the 
 current method and forget def: def is really only a way to define functions. Talking 
 about def here is not only wrong since you are referring to the definition of the 
 function instead of the function itself (you can also read 'method' here for 
 classmemberfunctions, but that would probably make it even more complex to read :) 
 but also this is not just about Python-scripts, but as well about DTML and Page 
 Templates... Do not think of them as functions, but really as scripts (what the 
 reference reffers to as 'methods') that return something instead of printing it... You 
 are probably right most of the times (I wouldn't know that actually) but the way you 
 state it is very confusing...

This will return a list of any File objects in the FileLib folder. Now, edit your index_html Page Template and add some more tal attributes to your mock-up:


...
<tr tal:repeat="item container/files">
  <td><a href="Sample.tgz" class="filename"
         tal:attributes="href item/getId"
         tal:content="item/getId">Sample.tgz</a></td>
  <td tal:content="item/content_type">application/x-gzip-compressed</td>
  <td tal:content="item/getSize">22 K</td>
  <td tal:content="item/bobobase_modification_time">2001/09/17</td>
</tr>
...
Anonymous User - May 31, 2002 2:14 am:
 Right here, or after the next paragraph, a careful and thorough explanation of how the 'magic' Python script
 just created is found and used by the tal:repeat is needed. For newbies to Zope starting right in with TAL
 and little/no Python experience this is otherwise a huge conceptual leap (I speak from personal experience).
Anonymous User - Sep. 10, 2002 3:35 pm:
 I agree whole-heartedly with the May 31 comment. This is not the first time in this book I found myself
 really wanting to know this.
Anonymous User - Sep. 22, 2002 7:14 am:
 Why do we need a python script at all?
 <tr tal:repeat="item python:container.objectValues(['File'])">
 Reconsider the order of presentation.
 But also: is this a Tutorial or just an Overview?
Anonymous User - Jan. 4, 2003 4:57 pm:
 It would be good to have a file in the directory to show also!!!!!

The interesting part is the tal:repeat attribute on the tr HTML tag. This attribute tells the template to iterate over the values returned by "container/files" (the Python script you created) and create a new table row for each of those files. During each iteration, the current file object being iterated over is assigned the name item.

The cells of each row all have tal:content attributes that describe the data that should go in each cell. During each iteration through the table row loop, the id, the content type, the size, and modification time replace the dummy data in the rows. Also notice how the anchor link dynamically points to the current file using tal:attributes to rewrite the href attribute.

This data comes from the item object by calling Zope API methods on what we know is a file object. The methods item/getId, item/content_type, item/getSize, item/bobobase_modification_time are all standard API functions that are documented in Zope's online help system.

Anonymous User - Apr. 27, 2002 9:46 pm:
 the method 'content_type' appears to be misnamed or excluded from the latest version, otherwise example
 works. API reference for 'file' confirms the syntax (though only by inference).
Anonymous User - May 16, 2002 11:02 am:
 In current File API it's getContentType
Anonymous User - Sep. 22, 2002 7:07 am:
 The methods getId, content_type, ... are in the API for File objects.
 Also, getID is a method, but isnt content_type a property?

XXXAmos: Perhaps refer also to the appendix in this book -TD

Go to Zope and test this script by first uploading some Files into the FileLib folder. This is done by selecting File from the add menu and clicking on the upload form button on the next screen. After uploading your file, you can just click Add. If you do not specify an id, then the filename of the file you are uploading will be used.

After uploading some files, go to the index_html Page Template and click the Test tab. Now, you can see the Page Template has rendered a very simple file library with just a few HTML tag attribute changes.

There are a few cosmetic problems with the file library as it stands. The size and date displays are not very pretty and don't match the format of the dummy content. You would like the size of the files to be displayed in K or MB rather than bytes. Here's a Python-based script that you can use for this:


## Script (Python) "file_size"
##
"""
Return a string describing the size of a file.
"""
bytes=context.getSize()
k=bytes/1024.0
mb=bytes/1048576.0
if mb > 1:
    return "%.2f MB" % mb
if k > 1:
    return "%d K" % k
return "%d bytes" % bytes
Anonymous User - June 11, 2002 4:08 pm:
 From what I see, the size is automagicly returned in K, at least in 2.5.1.
Anonymous User - June 11, 2002 4:20 pm:
 Nope, the above comment restuled from my mis-understanding of the paragraph above, since the paragraph says
"the size and date displays are not very pretty and don't match the format of the dummy content." However the
 dummy document is still in place. It should say "the size and date displays would not be very pretty, and
 wouldn't match the format[...]"

Create this script with the Id file_size in your FileLib folder. It calculates a file's size in kilobytes and megabytes and returns an appropriate string describing the size of the file. Now you can use the script in place of the item/getSize expression:


...
<td tal:content="item/file_size">22 K</td>
...

XXXAmos: Perhaps its interesting to explain what actually happens here, since file_size is not a method of the file object, but is an own created one. If I'm correct, it is that the item is pushed on top of the namespace and thus the usage of context refers to item, etc... -TD

You can also fix the date formatting problems with a little Python. Create a script named file_date in your FileLib folder:


## Script (Python) "file_date"
##
"""
Return modification date as string YYYY/MM/DD
"""
date=context.bobobase_modification_time()
return "%s/%s/%s" % (date.year(), date.mm(), date.day())
Anonymous User - May 23, 2002 3:11 pm:
 Should that be date.month() instead of date.mm()?
Anonymous User - May 23, 2002 3:25 pm:
 Well, mm() returns a zero-padded string while month() returns an integer. 

 >>> app.ZopeTime().mm()
 '05'
 >>> app.ZopeTime().month()
 5

 It could go either way.
Anonymous User - June 4, 2002 9:09 am:
 OK, but the month is now zero padded but the day isn't. Looks kinda funny, I.E 2002/06/5 Either use month or
 use the zero padded day too for consistancy. I'm not a Python'er so I don't have any idea how to get a zero
 padded day.
Anonymous User - June 11, 2002 6:06 pm:
 throretically:
 (date.year(), date.mm(), date.dd())

 however it's now the 11th and I'll have to wait until next month to find out for sure...  ;)

 "another newbie"
Anonymous User - Aug. 5, 2002 11:08 pm:
 In the book, I find that there are few named objects which mysteriously appear without explanation. In this
 case the "context" object in the script is what is mysterious.
 I understand that it is the readers responsibility to figure out what this is, but by going the traditional
 route of looking it up in the online documentation, I cannot find information about this "context" object.
Anonymous User - Nov. 7, 2002 6:23 am:
 how about time? how do you display it?
Anonymous User - Nov. 7, 2002 6:32 am:
 appendix B

Now replace the item/bobobase_modification_time expression with a reference to this script:


...
<td tal:content="item/file_date">2001/09/17</td>
...

Congratulations, you've successfully taken a mock-up and turned it into a dynamic Page Template. This example illustrates how Page Templates work well as the "presentation layer" to your applications. The Page Templates present the application logic (the Python-based scripts) and the application logic works with the data in your site (the files).

Remote Editing with FTP and WebDAV

You can edit Page Templates remotely with FTP and WebDAV, as well as HTTP PUT publishing. Using these methods, you can use Page Templates without leaving advanced WYSIWYG editors such as DreamWeaver.

The previous section showed you how to edit a page remotely using Amaya, which uses HTTP PUT to upload pages. You can do the same thing with FTP and WebDAV using the same steps.

  1. Create a Page Template in the Zope Management interface. You can name it with whatever file extension you wish. Many folks prefer .html, while others prefer .zpt. Note, some names such as index_html have special meanings to Zope.
  2. Retrieve the file using the URL of you page template plus /source.html or /source.xml. This gives you the source of your Page Template.
  3. Edit your file with your editor and then save it. When you save it you should use the same source URL you used to retrieve it.
  4. Optionally reload your page after you edit it, to check for error comments. See the next section for more details on debugging.

In later versions of Zope you'll probably be able to create Page Templates without using the Zope Management Interface.

BubbaFett - May 23, 2002 4:56 pm:
 I can reach source.html with a web browser, but not with WebDAV or ftp. FTP seems to get the source
 correctly. With WebDAV, apparently you need to enable a WebDAV source port and use it instead of your http
 port.
Anonymous User - June 4, 2002 9:12 am:
 2.Retrieve the file using the URL of you page template plus /source.html or /source.xml. This gives you the
 source
      of your Page Template. 

 "...URL of you ..." should be URL of your ..."
Anonymous User - Oct. 2, 2002 7:39 am:
To read the templates and not the modified HTML, use a WebDAV port. The trick is to set the "-W 9800" flag in
 the start.bat file and then use http://localhost:9800/<file>; as URL. This is explained in
 http://lists.zope.org/pipermail/zpt/2001-August/001892.html
 I've tried it, and I can now read template files into Mozilla, Frontpage, DreamWeaver without using the
 "/source.html"-extension (which is not accepted by the w2k "open-file" browser).

Debugging and Testing

Zope helps you find and correct problems in your Page Templates. Zope notices problem at two different times: when you're editing a Page Template, and when you're viewing a Page Template. Zope catches different types of problems when you're editing than when you're viewing a Page Template.

You're probably already familiar with trouble-shooting comments that Zope inserts into your Page Templates when it runs into problems. These comments tell you about problems that Zope finds while you're editing your templates. The sorts of problems that Zope finds when you're editing are mostly errors in your tal statements. For example:


<!-- Page Template Diagnostics
 Compilation failed
 TAL.TALDefs.TALError: bad TAL attribute: 'contents', at line 10, column 1
-->
BubbaFett - May 23, 2002 5:03 pm:
These do not show up as comments in your code, but rather under an "Errors" header on the ZPT Edit page.
Perhaps
 this behavior is from an older version.

This diagnostic message lets you know that you mistakenly used tal:contents rather than tal:content on line 10 of your template. Other diagnostic messages will tell you about problems with your template expressions and macros.

When you're using the Zope management interface to edit Page Templates it's easy to spot these diagnostic messages. However, if you're using WebDAV or FTP it's easy to miss these messages. For example, if you save a template to Zope with FTP, you won't get an FTP error telling you about the problem. In fact, you'll have to reload the template from Zope to see the diagnostic message. When using FTP and WebDAV it's a good idea to reload templates after you edit them to make sure that they don't contain diagnostic messages.

If you don't notice the diagnostic message and try to render a template with problems you'll see a message like this:


Error Type: RuntimeError
Error Value: Page Template hello.html has errors.

XXXAmos: Its PTRuntimeError -TD

That's your signal to reload the template and check out the diagnostic message.

In addition to diagnostic messages when editing, you'll occasionally get regular Zope errors when viewing a Page Template. These problems are usually due to problems in your template expressions. For example, you might get an error if an expression can't locate a variable:


Error Type: Undefined
Error Value: "unicorn" not found in "here/unicorn"

This error message tells you that it cannot find the unicorn variable which is referenced in the expression, here/unicorn. To help you figure out what went wrong, Zope includes information about the environment in the traceback. If you're in debugging mode this information will be available at the bottom of the error page. Otherwise, view the source of the error page to see the traceback. The traceback will include information about the environment:


...
'here': <Application instance at 01736F78>,
'modules': <Products.PageTemplates.ZRPythonExpr._SecureModuleImporter instance at 016E77FC>,
'nothing': None,
'options': {'args': ()},
'request': ...
'root': <Application instance at 01736F78>,
'template': <ZopePageTemplate instance at 01732978>,
'traverse_subpath': [],
'user': amos})
...

This information is a bit cryptic, but with a little detective work it can help you figure out what went wrong. In this case, it tells us that the here variable is an "Application instance". This means that it is the top-level Zope folder (notice how root variable is the same "Application instance"). Perhaps the problem is that you wanted to apply the template to a folder that had a unicorn property. The traceback doesn't provide a lot of help, but it can help you sometimes.

XXXAmos: ... had a unicorn property, but the folder to which you uploaded the template (in this case root) hasn't such a property. -TD

XML Templates

Another example of the flexibility of Page Templates is that they can dynamically render XML as well as HTML. For example, in Chapter 5, "Creating Basic Zope Applications", you created the following XML:


<guestbook>
  <entry>
    <comments>My comments</comments>
  </entry>
  <entry>
    <comments>I like your web page</comments>
  </entry>
  <entry>
    <comments>Please no blink tags</comments>
  </entry>
</guestbook>
viehmann - May 1, 2002 12:39 pm:
 There seem to be a lot of broken links... Also the number of the chapter is broken.
Anonymous User - June 4, 2002 9:17 am:
I did? I don't remember it. Ah! I see that is covered in Chapter 7, not 5. This is 5. You need to either move
 this example to after the guest book is created, or remove it.
Anonymous User - Sep. 22, 2002 7:30 am:
 its now chapter 6 http://www.zope.org/Documentation/Books/ZopeBook/current/SimpleExamples.stx
Anonymous User - Sep. 22, 2002 7:34 am:
 /you created/you will create/
 this is a forward reference. Again reconsider the order of presentation!

This XML was created by looping over all the DTML Documents in a folder and inserting their source into comment elements. In this section, we'll show you how to use Page Templates to generate this same XML.

Create a new Page Template called "entries.xml" in your guest book folder with the following contents:


<guestbook xmlns:tal="http://xml.zope.org/namespaces/tal">
  <entry tal:repeat="entry python:here.objectValues('DTML Document')">
    <comments tal:content="entry/document_src">Comment goes here...</comments>
  </entry>
</guestbook>
Anonymous User - Aug. 27, 2002 11:03 am:
 why is there a 'python:' word in the tal:repeat attribute? previously in the html examples it is not used in
 tal:repeat attributes

Make sure you set the content type to text/xml. Now, click Save Changes and click the Test tab. If you're using Netscape, it will prompt you to download an XML document, if you are using MSIE 5 or higher, you will be able to view the XML document in the browser.

Anonymous User - Jan. 24, 2003 11:57 am:
 For the most part, these examples have worked well. This time, when I try to change the content type to
 text/xml and click "save changes", I get an error:
 Zope has encountered an error while publishing this resource.
 Error Type: ImportError
 Error Value: cannot import name expat

 If I leave it as text/html, it saves fine, but test leaves me with a blank window.

Notice how the tal:repeat statement loops over all the DTML Documents. The tal:content statement inserts the source of each document into the comments element. The xmlns:tal attribute is an XML namespace declaration. It tells Zope that names that start with tal are Page Template commands. See Appendix C, "Zope Page Templates Reference" for more information about TAL and TALES XML namespaces.

Creating XML with Page Templates is almost exactly like creating HTML. The most important difference is that you must use explicit XML namespace declarations. Another difference is that you should set the content type to text/xml or whatever the content-type for your XML should be. The final difference is that you can browse the source of an XML template by going to source.xml rather than source.html.

XXXAmos: It would be good to refer to an explanation of explicit XML and why the user is restricted to using these -TD

Using Templates with Content

In general Zope supports content, presentation, and logic components. Page Templates are presentation components and they can be used to display content components.

Anonymous User - Aug. 3, 2002 5:25 am:
 I thought the whole point of page templates was to apply them to content for rendering. Surely then this
 deserves more coverage than two lines at the bottom of the chapter. It took me days to even work out how you
applied a template to content objects, and as for how to make the default rendering of an object be through a
 template, or how to prevent display of a content object unless it's through a template, well that's got me
 stumped.
Anonymous User - Sep. 6, 2002 8:53 pm:
 To apply a template to a content object, view the template in the context of the
 object. e.g. visit the url my_object/my_template.  The template will then be
able to access any of the object's attributes like <div tal:replace="here/some_property_or_method" />.
For
 example, you could view a CMF Document through a page template by having code like <div
 tal:replace="here/cookedBody" /> in the template.
 Your other questions are trickier. TO make the default rendering of an object be through a template: well,
 what kind of object? Folders of course you can do by defining index_html as a template; custom Zope products
 you can do likewise. But for callable objects like DTML Methods, you can't force it to render itself through
 a template, except (again) by calling the template in the context of the doc. You could access the object's
 entire content in the template like <div tal:replace="python:here()" />.
 I don't know how you'd "prevent display of a content object unless it's through a template". I was going to
 suggest proxy roles, but templates don't have proxy roles.

Zope 2.5 ships with several content components: ZSQL Methods, Files, and Images. DTML Documents and methods are not really pure content components since they can hold content and execute DTML code. As this time Zope doesn't come with a good general purpose content object. You can use Files for textual content since you can edit the contents of Files if the file is less than 64K and contains text. However, the File object is pretty basic.

Anonymous User - Sep. 22, 2002 7:42 am:
 /DTML Documents and methods/DTML Documents and DTML Methods/
 how abt a glossary and meta_type names in a different font? (Capitalize not enough)

Zope's Content Management Framework (CMF) solves this problem by providing an assortment of rich content components. The CMF is Zope's content management add on. It introduces all kinds of enhancements including work-flow, skins, and content objects. The CMF makes a lot of use of Page Templates. A later release of Zope will probably include the CMF.

Anonymous User - May 17, 2002 11:20 am:
 Cool
Anonymous User - June 11, 2002 6:26 pm:
 meanwhile... where would one go to learn CMF????
Anonymous User - June 15, 2002 6:21 am:
 At http://cmf.zope.org/ you can learn about it.
Josemary - Jan. 11, 2004 11:08 am:
 I try to use ZPT for my web page in josemary.freezope.org and in josemary.sytes.net, but my version is not
 the same that you are using. Is some document explaining the diferences between version 2.5 and version 2.6?

Conclusion

Zope Page Templates help you build web pages for your web applications. Templates make it easier for you to use normal HTML tools and techniques to build web pages. They also provide convenient hooks to allow you to attach them to your applications. Page Templates help designers and programmers work together to produce web applications. In Chapter 9, "Advanced Page Templates" you will learn about powerful template techniques like Python expressions, and macros.

Anonymous User - Apr. 27, 2002 11:14 pm:
 I'm sold on the ease of using templates and that scripts should be used instead of complex DTML, but don't
 see where they fit in with normal DTML docs and methods - will templates ultimately replace them? Can they
 work together as in a DTML var plugs in a snippet of template so that TAL tags work with modular assembly of
 DTML pages? Is it vice versa - that templates get embedded with DTML calls. I presume a DTML-var tag is
 ineffective in a template and a TAL tag is ineffective in a DTML document or method - so how do you get the
 best of both?
Anonymous User - May 1, 2002 8:56 pm:
 Page templates are good for building HTML pages.  They cannot render non-SGML output.

 DTML is good for building unstructured pages (like SQL methods), and it can render almost anything.

 This is the essential difference between the two. There is overlap, but they are both going to stick around
 in Zope probably forever.
 You can call DTML methods from page templates and vice versa.
Anonymous User - May 31, 2002 2:27 am:
 The 'magic' python scripts used need either more careful explanation in this chapter or should be reserved
 for the advanced chapter.
Anonymous User - Sep. 22, 2002 7:48 am:
 Rather advanced: is it possible to mix dtml and tal in one doc and cascade processing?

Previous Page Up one Level Next Page Using Zope Page Templates Comments On/Off Table of Contents