Zope Page Templates (ZPT)

This is, without a doubt, the single most cool thing about Zope. By using ZPT, the web developer can make a dynamic page which is easily edited with an existing HTML editor.

For those of you not familiar with Zope, let's suppose you wanted to create some dynamic text on a webpage that looked like this:

This page has been viewed 23 times.

To do this, you would want to create code that generates the following HTML:

<p>This page has been viewed <b>23</b> times.</p>

In a programming language such as Perl or Python, you might write this as (Python syntax shown):

print "<p>This page has been viewed <b>%d</b> times.</p>" % PageCount

This is simple, sure, but in most web-development shops, the guys who can make a nice-looking web page are not the same guys who can make smooth-running code. In fact, the typical HTML developer couldn't code his way out of a wet paper bag, and the typical coder creates web pages that are so ugly that they frighten small children.

If you want a nice-looking page, then you really want your HTML developer to do it, and they will want to edit the webpages with a decent HTML tool, such as DreamWeaver.

The wildly-popular programming language PHP will get you a lot closer to where you need your web-development shop to be. PHP does this by letting you put code directly into a web page. With PHP, you could code something like:

<p>This page has been viewed <b><?php print $PageCount; ?></b> times.</p>

This is great because now your HTML developer can edit the PHP file with DreamWeaver. Everyone is happy now, right?

Well, almost. Although this solution is a million times better than coding a web page in a Perl or Python program, you're now facing a couple unpleasant problems.

First off, this block of PHP code isn't going to render very nicely in DreamWeaver. DreamWeaver recognizes that <?php and ?> enclose a block of executable code, but it can't guess just how this will look when the user browses the page. Instead of showing the developer "23", DreamWeaver displays the entire code block as a little yellow icon.

For something as simple as a page count, this is just fine. The HTML developer knows what the icon means and can mentally replace it with the text. But what if the PHP code does something more complex? What if it creates an entire table of content? Can your HTML developer make a nice-looking page when 30 square inches of screen real-estate are compressed down to a single yellow icon?

The second problem with mixing code and HTML in a single file is that you're mixing code and HTML in a single file. You now have two very different sets of designers editing the same file. This is a recipe for disaster.

Not only do you have the added risk of one group removing the other group's changes, there is the constant risk that the HTML designers will break the code by re-arranging the HTML to make it look better; and the risk that the coders will make a page look bad by re-arranging it to make it run better.

The effects of this second problem can be limited by implementing some strict coding standards that limit how much code actually appears in the HTML file, but to some extent, sloppy code is inevitable. Sooner or later you will get bitten.

ZPT tackles both problems with a very clever solution. With ZPT, you would create HTML such as:

<p>This page has been viewed <b tal:content="PageCount">23</b> times.</p>

DreamWeaver won't recognize the tal:content attribute, so it will completely ignore it and render the page normally. Then when the page is surfed by a user, Zope will recognize the tal:content attribute and replace the 23 with the correct number identified by the symbol PageCount.

Zope will only allow you to put very simplistic code in a web page, so this forces the coders to keep their code elsewhere.

In a large shop, you can enforce this separation by granting edit rights on the code only to the coders, and granting edit rights on the HTML only to the HTML designers.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.