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

Dear Monks,

I've put together two scripts that use Data::Table. Running the two scripts results in html files with well calculated nicely formatted tables.

In order to present those two files with tables with the same header and footer I've been looking for solutions. Amongst others, File::Cat, Templates (TT) or HTML::Template. I'm puzzled and don't really know where to start.

I thought to use the TT as it has the possibility to include 'files' (meaning my tables). Couldn't find out wether this is possible with HTML::Template.

The files I'd like to `include` should be pointed to as a variable as I never know upfront how many and what names they have.

Any ideas to point me in the right direction are welcome.
Gert

Replies are listed 'Best First'.
Re: working with templates
by samtregar (Abbot) on Apr 24, 2007 at 18:58 UTC
    It's easy to include the results of other modules using HTML::Template. In the table:

      <tmpl_var name="my_table">

    In the code:

    # setup the table... $table = Data::Table->new(...); ... # send the result to the template: $template->param(my_table => $table->html);

    -sam

      hi,
      Thanks for your response. I can't get it to work. But I'll keep trying. Just checking about the template file. Should I put some code to let HTML::Template know which template I want to use? eg.
      my $template = HTML::Template->new( filename => 'indexpag2.tpl', loop_context_vars => 1 );
        alright thanks it works:
        #!/usr/bin/perl -w use strict; use diagnostics; use Data::Table; use HTML::Template; open( OUT, ">itworks.html" ) or die "cant open out $!"; my $table = new Data::Table( [ [ 1, 2, 3 ], [ 10, 20, 30 ] ], [ 'A', ' +B' ], 1 ); my @newCol2 = (); $newCol2[ $table->nofRow - 1 ] = undef; # print $table->html; my $template = HTML::Template->new( filename => 'template.tpl' # loop_context_vars => 1, # filter => \&cstmrow_filter ); $template->param( my_table => $table->html ); print OUT $template->output;
        template.tpl
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" +> <title>It Works</title> <meta name="generator" content="TextMate http://macromates.com/"> </head> <body bgcolor="silver"> <center> <h1 id="just_a_sample_table">Just a sample table</h1> <tmpl_var name="my_table"> <pre>2007-04-24</pre> &copy; ... ; ) </center> </body> </html>
        THANKS!
Re: working with templates
by friedo (Prior) on Apr 24, 2007 at 19:00 UTC

    HTML::Template has the TMPL_INCLUDE tag which can include a static file. However, I do not think you can use a variable include filename with H::T.

    You can do it with Template Toolkit, though. If you've got a template variable called 'filename' then [% INCLUDE $filename %] will work.

      thanks,
      Working with these template variables is a bit over the top of my head at the moment. Couldn't figure out how to code that. Well I'll work harder and look for the most simple solution.
      Gert
      HTML::Template has the TMPL_INCLUDE tag which can include a static file. However, I do not think you can use a variable include filename with H::T.
      you can do this with HTML::Template::Compiled: INCLUDE_VAR