fallenmonkey has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to put together a dynamic web page that generates data and injects it into a template. My HTML template is laid out in this form:
(html stuff) $header (html stuff) $main_content (html stuff) $footer
I'm reading that file into my Perl program, and generating data in (for example) the $header, $main_content and $footer variables. My goal is to do a simple read of the template and spit it out again, interpolating variables along the way. I'd like to avoid pattern matches on each line and such. Can this be done feasibly? I seek your wisdom, O wise monks, and am grateful for any that you can give me!

-- The Fallen Monkey

Replies are listed 'Best First'.
Re: Forcing Variable Interpolation
by MrCromeDome (Deacon) on May 09, 2002 at 18:41 UTC
    Am I understanding this correctly? The template is a file of your own creation, and using perl you read each line of the template, look for $header (for example), and if you find it, drop in your data? If so, you're reinventing the wheel ;) There's a nifty module called HTML::Template that can help you.

    A template with HTML::Template takes the following format:

    <html> <head> <title><TMPL_VAR NAME=TITLE></title> </head> <body bgcolor="#FFFFFF"> <TMPL_VAR NAME=BODYTEXT> </body> </html>
    Each TMPL_VAR entry is similar to $header, $footer, etc. You can then populate your template as easily as this:
    my $tmpl_frame = HTML::Template->new(filename => "/path/to/my/template.tmpl"); $tmpl_frame->param( TITLE => "Sample Page", BODYTEXT => "This is some text." ); print $tmpl_frame->output;
    Cool, eh? ;)

    Hope this helps,
    MrCromeDome

      Woohoo! For some reason I didn't even think about using an existing module! Thanks so much, that makes so much more sense :P

      -- The Fallen Monkey

Re: Forcing Variable Interpolation
by Ovid (Cardinal) on May 09, 2002 at 18:44 UTC

    What template system are you using? If you're rolling your own, you're likely to start hitting serious limitations on what you would like to accomplish. My personal favorite is Template Toolkit. Here's a sample of how it works:

    use strict; use Template; $|++; my $template = Template->new( { INCLUDE_PATH => '..\templates' } ); my %data = ( name => 'Ovid', colors => [qw/ red black green /] ); $template->process( 'some.tmpl', $data ) or die $template->error();

    And then, in some.tmpl:

    Hi [% name %] Here are some colors: [% FOREACH color = colors; color; "\n"; END %]

    There are other templating systems, I just happen to find this one the most useful (and it's not tied to HTML). Check the above URL for more information.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Forcing Variable Interpolation
by grep (Monsignor) on May 09, 2002 at 18:42 UTC
    You might want to focus your efforts on one of the already built (and tried and tested) solutions for HTML templating.
  • Mason
  • HTML::Template
  • Template Toolkit
  • This saves you the time to write the real focus of your application



    grep
    Unix - where you can throw the manual on the keyboard and get a command
Re: Forcing Variable Interpolation
by CukiMnstr (Deacon) on May 09, 2002 at 19:01 UTC
    another thing that might prove useful is a comparaison between the different templating systems avaible here at the monastery, you might find something interesting in it.

    hope this helps,