Tantalizing Templates: Build-a-Pizza Meets the Smallville Gazette
July 12, 1999
At its core, last month's
Build-a-Pizza script
took input from form fields and constructed an on-the-fly
web page. Rather than output its HTML directly to the
browser, though, we could redirect the Build-a-Pizza script
to generate its HTML to a scalar variable, say,
$pizzaHTML. Using the search-and-replace template
technique we've already seen, then, it is a small leap to
insert the Build-a-Pizza content inside the Smallville
Gazette.
Integrating Build-a-Pizza into smallville.cgi results
in one somewhat long script which we can only explain in
pieces, but you can
view
the whole script here.
To begin with, we pasted the Build-a-Pizza Perl script at
the end of the smallville.cgi script seen earlier.
Three changes were then made to the pizza script so that
it could integrate into the Smallville script:
- Instead of print output, all output strings were
assigned to the variable $pizzaHTML. For example,
print $theform;
became
$pizzaHTML.=$theform;
This way, the HTML output generated by the pizza script
builds onto the variable $pizzaHTML rather than
being sent directly to the browser.
- We removed calls to $cgiobject->header from the
pizza script because an HTML header is already generated
at the top of the Smallville script.
- The main routine of the pizza script which calls the
various pizza subroutines was placed into a subroutine
of its own, named &build_a_pizza. This simply
lets us execute the pizza script with a subroutine call,
thus triggering all the code which builds content into
$pizzaHTML.
Recalling the first incarnation of smallville.cgi,
just after we search-and-replace on the live date
and before we output the final page, a few simple lines
trigger the Build-a-Pizza substitution:
#generate pizza form
&build_a_pizza;
#search-and-replace on pizza form
$resultPage=~s/INSERT PIZZA HERE/$pizzaHTML/g;
The new smallville.cgi, then, features both the live
date and Build-a-Pizza integrated into the Smallville
Gazette template based on our relatively simple
search-and-replace technique.
Tantalizing Templates: Live Date
The Perl You Need to Know
Conclusion
|