[Zope] - Zope Example Formatting Field data indirectly via a TinyTable

Brad Clements bkc@murkworks.com
Wed, 20 Jan 1999 16:29:45 -0500


Here's an example I thought you all might be interested in. Note that this is 
supposed to be Structured Text, however when trying to view it as such Zope 
reports an error with 'identifier' (seems it sees _['identifier'] for evaluation even 
though it's in "example code")

Also, when I try to upload this, the upload process dies on the first <!--#in--> 
in the text. 

Btw, I don't know anything about python, very little about html and I've spent 
only about four days setting up Zope on my web site, most of that time was 
lurking in this list and being a pest. ;-)


Anyway, here's the report.


* Formatting Record Fields Indirectly Using TinyTable Data *

While constructing my Zope based web site, I encountered an annoying 
problem displaying record field data. The problem: there are many record 
fields, I'm not sure what order I want to list them in, and I'm not sure 
how I want to format the fields. If I immediately began constructing an 
html table, it would take a lot of effort to re-arrange the table format 
or change the attributes.

I devised a neat scheme to dynamically format the record data using a 
TinyTable that contains the names of each field variable, description 
and formatting information. I use two <!--#in--> dhtml commands, the 
first selects the desired record from my database, and the second 
selects formatting data from the TinyTable.

This method allows me to arrange the data fields along columns or rows, 
or without even using a table at all. In my case, I chose to display 
record data in a table, where each field is in it's own row. The table 
output looks like this::

Owner's Name     | Village Of Potsdam  
Address          | Box 5168  
                 | Potsdam, Ny 13676  
Phone            | 315-265-7480  
Manager's Name   | Brad Clements  
Address          | Box 5168  
                 | Potsdam, Ny 13676  
Phone            | 315-268-1000x11  
Email Address    | bkc@murkworks.com  
Airport Web Site | www.air.gen.ny.us

The format of the TinyTable follows::

Id: AirportInfoFormatTable 
Title: Used to format AirportInfo display   
Columns:  dbvar description format rowspan color
Delimiter: :   
Data:
OWN_NAME:Owner's Name:capwords::EvenColor
OWN_ADD:Address:capwords:2:OddColor
OWN_CSZ::capwords::OddColor
OWN_PHN:Phone:asis::EvenColor
MGR_NAME:Manager's Name:capwords::OddColor
MGR_ADD:Address:capwords:2:EvenColor
MGR_CSZ::capwords::EvenColor
MGR_PHN:Phone:asis::OddColor
MAILTO:Email Address:mailto::EvenColor
URL:Airport Web Site:url::OddColor

The TinyTable "AirportInfoFormatTable" defines the following columns
dbvar -- The field name in the master database
description -- Descriptive text 
format -- Controls how the master data is formatted
rowspan -- Causes the description to span rows
color -- Specifies the html class for each row

A ZSQL Method is used to retrieve data from the master database using a 
select call through a ZODBC database connection. In the example code 
that follows, 'getall=1' is used to signal the ZSQL Method 
'AirportLookup' to return all fields (i.e., select *).

In the DHTML document code that follows, note the use of the _['name'] 
dhtml construct. This is replaced with the contents of the variable 
called 'name', however we're using _[dbvar]. Dbvar is a variable from 
our TinyTable, which contains each of the field names in turn, such as 
OWN_NAME. So _[dbvar] is replaced with the contents of the variable 
OWN_NAME when dbvar equals OWN_NAME. This is an indirect lookup of 
the field contents.

Also, in the following example text, size=1 is used to retrieve only the 
first record from the ZSQL method. In my case, I only want the first 
record. Also, if the master data field is blank, we don't list it in the output
table::

<!--#if expr="AirportLookup(getall='1',identifier=_['identifier'])"-->
<!--#in expr="AirportLookup(getall='1',identifier=_['identifier'])" 
size=1-->
<table width=80% border=0>
<!--#in AirportInfoFormatTable-->
<!--#if "_[dbvar]"-->
<!--#if description-->
<td 
<!--#if rowspan-->rowspan=<!--#var rowspan--><!--#endif-->
<!--#if color-->class=<!--#var color--><!--#endif-->
><b><!--#var description--></b></td>
<!--#endif-->
<td align=left>
<!--#if "format == 'capwords'"-->
      <!--#var "_.string.capwords(_[dbvar])"-->
<!--#elif "format == 'url'"-->
      <A href="http://<!--#var "_[dbvar]"-->">
        <!--#var "_[dbvar]" url_quote--></a>
<!--#elif "format == 'mailto'"-->
      <A href="mailto:<!--#var "_[dbvar]"-->"><!--#var "_[dbvar]"--></a>
<!--#else-->
      <!--#var "_[dbvar]"-->
<!--#endif-->
</td>
</tr>
<!--#endif-->
<!--#/in-->
</table>
<!--#/in-->
<!--#else-->
<!--#var AirportLocator-->
<!--#endif-->



Brad Clements,                bkc@murkworks.com   (315)268-1000
http://www.murkworks.com                          (315)268-9812 Fax
netmeeting: ils://ils.murkworks.com               ICQ: 14856937

We must come down from our heights, and leave our straight 
paths, for the byways and low places of life, if we would 
learn truths by strong contrasts; and in hovels, in forecastles, 
and among our own outcasts in foreign lands, see what has been 
wrought upon our fellow-creatures by accident, hardship, or vice. 
- Richard Henry Dana, Jr. 1836