Lesson 9. Elvis, Up Close and Personal

Zope cannot find the tutorial examples. You should install the tutorial examples before continuing. Choose "Zope Tutorial" from the product add list in the Zope management screen to install the examples.

If you have already installed the tutorial, you can either follow along manually, or reinstall the tutorial examples. Note: make sure that you have cookies turned on in your browser.

People who come to your site want a personal relationship with Elvis. You can provide this by tailoring your site to the needs and preferences of your viewers. Let's add the ability to bring new Elvis sightings to the attention of site visitors. This way when a visitor comes to your site they'll immediately know which sightings are new since they last visited.
  1. Click the sightings.html template to edit it.
  2. Click the Test tab to view it.

All the sightings should be marked as new.

  1. Reload the page.

Now none of the sightings should be marked as new. This is because you've already seen them.

  1. Click the sightingsFolder folder to enter it.
  2. Choose File from the "Select type to add..." list.
  3. Type Seattle for the id.
  4. Click the Add button.
  5. Now click on the new file to edit it.
  6. Change the content type to "text/html'.
  7. Change the contents of the file to:
    <p>6/1/2003 -- Seattle</p>
    <p>Elvis spotted spare changing on Broadway.
    He denied being the King.</p>
  8. Click the Save Changes button.

Now you've created a new sighting. Let's see if it is marked as new by the sightings page.

  1. Click the sightings.html page in the lesson9 folder.
  2. Click the Test tab.

Sure enough our new sighting is marked new.

How does this work? It uses HTTP Cookies. When you visit the sightings.html page a cookie is set that records the current time. Then each time you return to the page sightings that are newer than the cookie will be marked as new.

Let's look at how this works.

  1. Click the sightings.html template to view its contents.

Notice that the page uses the tal:define statement to defines a lastVisit variable. It is defined by calling the lastVisit.py script. This variable holds the time that the user last visited the page as recorded in a cookie.

Later in the template this time is compared to the modification time of each sighting object. If the sighting is new then it is marked as such by including a bold tag. The bold tag uses the tal:condition statement to optionally include its contents. In this case the condition checks to see whether the bobobase_modification_date of the sighting is newer than the last visit. This strangely named method keeps track of the last modification date of Zope objects.

Now let's examine the lastVisit.py script.

  1. Click on the lastVisit.py script to view it.

Notice how it includes comments explaining its functioning. Comments begin with a number sign. The script does two things: it retrieves a cookie from the request, and it sets a new cookie using the response.

Summary

You can use cookies to personalize a web page. Zope templates can dynamically control their presentation.

In the next lesson you'll learn how to create a mail feedback form.