>I'm trying to customize the look of a guestbook show page on an NT server.
>
>[ code snipped ]
hmm..
if that's all there is in the script, it has some rather serious
problems.
for starters, it calls the function &PrintHeader() in the second
line, but that function isn't defined anywhere in the script you
posted. OTOH, the function &ShowError() *is* defined, but
never called. also, unless the text of the data file is
formatted as HTML, you're just going to get a single mass of
text inside the <blockquote>.
the basic structure of the script is to build a webpage in three
pieces.. first it prints everything in the page up to where the
content should be, then it opens the data file and prints that,
then it prints the rest of tags which close off a page. it's a
fairly common way to do dynamic pages, but personally, i don't
like it much. it puts too much page-specific data into the
script, and makes editing the page's appearance difficult for a
normal user.
my own preference is to put the information about how the
output is supposed to look off in a separate file. it
makes the script a bit cleaner and much easier to re-use, and
makes it possible for a non-programmer to change the way the
page will look.
a simple version of that type of thing, which should probably
work with the data file you've already got, is tacked on below.
it's very similar to the one i posted for Brent the other day,
but this version works as a standalone.. no library files needed.
to make it work, you *do* need another file, which is basically a
blank version of the output page. all your images, formatting,
and other layout can be done in that file, and changed at will.
all you need to do is toss in a tag that says:
== DATA ==
where you want the contents of the guestbook file to go, and the
script will paste the dynamic content in there.
(frankly, this is just a script version of the SSI #include
directive, but hey, whatever works)
---- mk_page.pl ----
#!/usr/local/bin/perl
$DATA_FILE = "guestbook.txt";
$TMPL_FILE = "template.html";
$tmpl = &read_file ( $TMPL_FILE );
$hash = {};
$hash->{'data'} = &read_file ( $DATA_FILE );
print "Content-type: text/html\n\n";
print &edit_template ($tmpl, $data);
exit (0);
#### SUBROUTINES #############################################
# edit_template ( template, hash )
#
# takes a tagged string and a hash of names/values as its input,
# and replaces all the tags in the template with corresponding
# values from the hash.
#
# tags are of the form:
#
# == TAG_NAME ==
#
# where the name can be any series of non-whitespace printing
# characters. the function ignores when it matches tags, so the
# tags:
#
# == TAG_NAME ==
# == tag_name ==
# == Tag_Name ==
#
# would all be replaced with the same value.
sub edit_template {
my $tmpl = shift;
my $hash = shift;
for $item ( keys %$hash ) {
$tmpl =~ s/==\s+$item\s+==/$hash->{ $item }/gi;
}
$tmpl =~ s/==\s+\w+\s+==//g;
return $tmpl;
}
# read_file ( filepath )
#
# takes a filepath as input, and tries to read the contents into a
# single, scalar variable. if it succeeds, it returns that
# scalar. if it can't open the file, it calls a library routine
# to report the error in a way the web browser can display.
sub read_file {
my $file = shift;
my $tmp = $/;
undef $/;
open (FILE, "./$file") or
&error ( 'File', qq( Couldn't open "$file": $!) );
my $data = <FILE>;
close FILE;
$/ = $tmp;
return $data;
}
# error ( type, message )
#
# prints an error message in a form which can be understood by a
# web browser, then shuts down the program.
sub error {
my $type = shift;
my $message = join ("\n", @_);
print <<__done;
Content-type: text/html
<head><title>$type Error</title></head>
<body>
<h1>510 $type Error</h1>
$message
</body>
__done
exit (1);
}
mike stone <[EMAIL PROTECTED]> 'net geek..
been there, done that, have network, will travel.