Tantalizing Templates: Live Date
July 12, 1999
Last month, in
Part 3 "Maintaining State",
we created a Perl-based
live pizza order form.
While "Build-A-Pizza" certainly worked, it was
rather, well, plain looking. For all of the reasons we've
seen so far in this article, constructing a complex Web page
with on-the-fly HTML would be a laborious task. Not so fast!
Let's build a template, shall we?
Using our HTML editor of choice -- whether a text editor or
a fancy spiffy visual editor -- we've made a mockup of the
web page that our Perl script should deliver. Welcome to
Smallville!
Artistic shortcomings aside, the Smallville Gazette template
is a large table structure whose HTML is quite
messy and long -- we wouldn't want to manually output
this page using Perl print statements! Yet, we want
to serve this page live using Perl to generate two
on-the-fly elements: the current date and the "Build A
Pizza" form that we saw in Part 3 of this series.
There are certainly other ways to insert a live date into
a web page, but we'll use Perl to help illustrate
our template technique with a simple example. From there we
can move onto the pizza form. Notice how we've built
the template with two placeholders:
"INSERT DATE HERE" and
"INSERT PIZZA HERE". In fact, these
placeholders could be any text that is unique -- in other
words, text which doesn't genuinely appear elsewhere
in the page. More commonly we might use HTML comment tags
to create the placeholders, such as:
<--INSERT DATE HERE-->
<--INSERT PIZZA HERE-->
Think about the search-and-replace function in text editors
and word processors. That is essentially what we're
going to do with Perl and the above template page. Our
Perl script will read in the HTML for the template page
and simply insert the on-the-fly content in place of the
placeholders. We'll do this first only with the live date,
to keep things simple.
smallville.cgi
#!/usr/local/bin/perl
use CGI;
$cgiobject=new CGI;
print $cgiobject->header;
#read in template page
$templateFile="smallville.html";
open(INFILE,"<$templateFile");
@templatePage=<INFILE>;
close(INFILE);
#condense page array into one scalar variable
$resultPage=join("",@templatePage);
#determine current date
($sec,$min,$hour,$mday,
$mon,$year,$wday,$yday,$isdst)=localtime(time);
$curDay=(Sunday,Monday,Tuesday,Wednesday,
Thursday,Friday,Saturday)[$wday];
$curMonth=(January,February,March,April,
May,June,July,August,September,
October,November,December)[$mon];
$liveDate="$curDay, $curMonth $mday, ".($year+1900);
#search-and-replace on date
$resultPage=~s/INSERT DATE HERE/$liveDate/g;
#done, output page to browser
print $resultPage;
|
After a familiar beginning the script needs to read in the
template page, in this case assuming that page is
named smallville.html. Perl opens this template file
and reads its entire contents into the array
@templatePage, then closes the input file. By reading
the entire file into an array, each line of the file is
stored as an element of that array. For our purposes,
we'd actually like the entire HTML document to reside
in a single scalar variable, so the Perl join
function is used to concatenate each line into one long
string of HTML. Notice how we join the lines with a null
separator, "".
The entire HTML for the Smallville Gazette template now
resides safely in $resultPage. We follow a short
tangent now to calculate the current date in a desirable
format ... specifically, we grab the date, day name, and
month name and build a string $liveDate in the
form "January 1, 1999". Notice that when Perl
reports the year it subtracts 1900 -- thus, the year 1999
is reported as 99. Hence, we add 1900 to the year for display.
Is this a Y2K bug? No, because in the year 2000, Perl will
report the year as 100. Adding 1900 to 100 gives us
2000. Whew!
The search-and-replace occurs in one simple statement: a
regular expression substitution, the kind we saw in
Part 2 of "The Perl You Need to Know". We search
the value of $resultPage for an instance of "INSERT
DATE HERE". When found, this match is replaced with the
value of $liveDate, which of course contains
the current date. The g modifier at the end of the
substitution replaces all instances of "INSERT DATE
HERE", in case we placed this placeholder in multiple
locations in the page. Omitting the g modifier
would have only replaced the first placeholder to be found.
If we had used an HTML comment tag as the placeholder,
rather than literal text, the substitution might have
looked something like this:
$resultPage=~s/<--INSERT DATE HERE-->/$liveDate/g;
Finally, the print statements outputs the entire
HTML document to the browser, serving the newly generated
page:
That may seem like quite a bit of explanation to simply
insert the date into our template page -- and it was.
But we're just laying the groundwork because this template
technique can be used for far more complex scenarios
of generated live content. In fact, let's now insert the
live "Build-a-Pizza" example from Part 3 into
the Smallville Gazette. Talk about fun!
CGI.pm, the Middleman
The Perl You Need to Know
Tantalizing Templates: Build-a-Pizza Meets the Smallville Gazette
|