in reply to Design vs. Code vs. Maintenance

I used to do this all the time by hand without using any CPAN modules. Here is my method:

  1. I named my html templates with a .htmlt extension, so I'd know they were templates.
  2. My variable placeholders in the template files were designated as allcap names of their matching perl variable name. Example, if I had $title in my .cgi, then I'd have a $TITLE in my .htmlt.
  3. In my .cgi, I'd have the following:
    open(TEMPLATE, "mypage.htmlt") or die "can't open mypage.htmlt: $! +"; local($/) = undef; my $template = <TEMPLATE>; close(TEMPLATE); $template =~ s/\$TITLE/$title/g; print "Content-type: text/html\n\n"; print $template;
That's about all it took. No modules, no special installs or permissions with the ISP.

Replies are listed 'Best First'.
RE: RE: Design vs. Code vs. Maintenance
by BBQ (Curate) on May 19, 2000 at 05:32 UTC
    Yeah, I know what you're saying... This is what I have (and what I'm trying to run away from):
    # parses HTML files as templates ################################################# sub MakeHTML { my ($file,%HTML) = @_; my ($line,$tag,$html); # open HTML file open(READ,$file) || return(0); foreach $line (<READ>) { foreach $tag (keys %HTML) { # substitute tags $line =~ s/\[$tag\]/$HTML{$tag}/gi; } # gather parsed HTML $html .= $line; } close(READ); return($html); } # assign things like $STUFF{'myname'} = 'John Burbridge'; $STUFF{'myage'} = 26; # then print it my $html = MakeHTML('/path/to/template.htm',$STUFF) or Errors('couldnt parse the template'); print "Content-type: text/html\n\n"; print $html;
    and the template file would look like this:
    <html><body> My name is [myname] and I am [myage] years old. </body></html>
    Sure it looks pretty simple and handy at 1st glance, but beleive me: ITS A BAD HABIT. I had to learn this the hard way...