- A Simple Template Using Perl Server Pages (PSP)
- The <tag> Tag and page.psp Template
- Enhancing Templates
- About This Article
The <tag> Tag and page.psp Template
The template used by Listing 1 has to define the <template> tag as well as provide the template HTML to complete the page. An example of a template that does both is shown in Listing 2, which provides some simple structure to the page and defines basic navigation as well. Note again that this page has to be viewable as HTML in a standard Web browser or in graphic HTML tools. As a result, extra care has been taken to make sure that the template logic doesn't interfere with the basic structure of the HTML in the template.
Listing 2-Template Definition (page.psp)
01 <tag name="template" accepts="title, section" body="1"> 02 <output> 03 <html> 04 <head> 05 <title>VeloMeter - $title</title> 06 </head> 07 08 <body bgcolor="white"> 09 <table width="80%" align="Center" cellspacing="0" cellpadding="0" border="0"> 10 <tr> 11 <td colspan="3"><a href="/"><img src="/images/logo-velometer-200.png" alt="VeloMeter" border="0" /=""></a></td> 12 </tr> 13 <tr> 14 <td colspan="3" align="Right"><font face="Arial, Helvetica" size="2"><a href="/download.psp">download</a> . <a href="/features.psp">features</a> . <a href="/screenshots.psp"> 15 screen shots</a> . <a href="/forum/">forum</a></font></td> 16 </tr> 17 <tr> 18 <td colspan="3" bgcolor="#333399"><font face="Helvetica, Arial, sans- serif"><img src="/images/dot-black.gif" width="2" height="2" border="0" /=""></font></td> 19 </tr> 20 <tr> 21 <td valign="top" colspan="3"> 22 <table width="100%" cellpadding="10" cellspacing="0" border="0"> 23 <tr> 24 <td> 25 <h2>$title</h2> 26 $body 27 </td> 28 </tr> 29 30 </table> 31 </td> 32 </tr> 33 </table> 34 35 <p align="center"><font face="Arial, Helvetica" size="1">©2001 36 <a href="http://www.velocigen.com/">VelociGen Inc</a></font></p> 37 </body> 38 </html> 39 </output> 40 </tag>
In PSP, a custom HTML tag is defined by using another custom tag, called <tag>. This tag can contain any valid PSP page content, which then is executed after the tag is used. In Listing 2, the <tag> tag is used in line 01 to define a tag called template by specifying the name of the tag in the name attribute of the <tag> tag. This name could be any valid XML string, but a good rule of thumb is to avoid names already used by HTML elements. Another attribute of the <tag> tag is accepts, which defines a list of variables that the tag defines in terms of specified attributes. In this case, the <template> tag accepts attributes called title and section that are assigned to variables with the same names. These variables are lexically scoped to their assigned tag, so multiple tags can use the same variable/attribute name without causing overlap or unusual behavior. The last attribute of <tag> is body, which indicates that the contents of the <template> element should be accessible within a special variable called $body. The <tag> tag ends on line 42, the last line of the template.
The variables defined within the scope of the <template> tag$title, $body, and $sectioncan be called directly within the template text by using the <output> tag in lines 02 and 41. Everything within <output> then is treated like a double-quoted string in Perl. Lines 05 and 27 use the $title variable to display the specified title, for instance, and line 28 places the $body variable in context. That single line represents the entire result of any Web application or static file that invokes the <template> tag, just as the <template> tag itself represents this template in those files. These two constructs provide the abstracted interface between templates and Web applications that then can be extended across the site.