Perl Programming Page 4 - Looping, Security, and Templating Tools |
HTML formatting is slightly different from plaintext formatting--there are essentially two main schools of thought. The first, used by HTML::Template, is similar to the method we saw in Text::Template; the template is stored somewhere, and a Perl program grabs it and fills it in. The other school of thought is represented by HTML::Mason, which we'll look at next; this is inside-out--instead of running a Perl program that prints out a load of HTML, you create an HTML file that contains embedded snippets of Perl and run that. To compare these two approaches, we're going to build the same application in HTML::Template, HTML::Mason, and Template Toolkit, an aggregator of RSS (Remote Site Summary) feeds to grab headlines from various web sites and push them onto a single page. (Similar to Amphetadesk, http://www.disobey.com/amphetadesk/, and O'Reilly's Meerkat, http://www.oreillynet.com/meerkat/.) RSS is an XML-based format for providing details of individual items on a site; it's generally used for providing a feed of stories from news sites. Variables and Conditions First, though, we'll take a brief look at how HTML::Template does its stuff, how to get values into it, and how to get HTML out. As with Text::Template, templates are specified in separate files. HTML::Template's templates are ordinary HTML files, but with a few special tags. The most important of these is <TMPL_VAR>, which is replaced by the contents of a Perl variable. For instance, here's a very simple page: <html> When filled in with the appropriate details, this should output something like: <html> In order to fill in those values, we write a little CGI program similar to the following one: use strict; my $template = HTML::Template->new(filename => "catalogue.tmpl"); $template->param( PRODUCT => "World's Biggest Enchilada" ); print "Content-Type: text/html\n\n", $template->output; Again, as with Text::Template, our driver program is very simple--load up the template, fill in the values, produce it. However, there are a few other things we can do with our templating language, and hence there are a few other tags that allow us a little more flexibility. For instance, suppose we happen to have a picture of the world's biggest enchilada--that would be something worth putting on our web page. However, we don't have pictures for everything in the database; we want to output a pictures section only if we actually do have an image file kicking about. So, we could add something like this to our template: <TMPL_IF NAME=PICTURE_URL> This means that if PICTURE_URL happens to have a true value--that is, if we've given it something like a real URL--then we include the photo <DIV>. As these <TMPL_...> tags are not real HTML tags, only things processed by HTML::Template, it's not a problem to stick one in the middle of another HTML tag, as we have here with <IMG SRC="...">. Of course, if we don't have a picture, we might want to stick another one in its place, which we can do with the <TMPL_ELSE> pseudotag: <div class="photo"> Notice that although our <TMPL_IF> must be matched by a </TMPL_IF>, <TMPL_ELSE> is not matched. But perhaps we're being unduly complex; all we need in this example is a default value for our PICTURE_URL, and we can do this directly with a DEFAULT attribute to <TMPL_VALUE>: <div class="photo"> Validation
Please check back next week for the continuation of this article.
blog comments powered by Disqus |
|
|
|
|
|
|
|