[Thamesvalley-pm] generating HTML

Andy Wardley abw at wardley.org
Thu Oct 18 11:13:32 PDT 2007

Greg Matthews wrote:
> Is there a neater way of generating html than this:

You can go down the templating route, using either the Template
Toolkit (TT), HTML::Template, or one of the many other template modules.

Here it is using TT:

   use Template;

   # create a TT processor and tell it where your templates are
   my $tt   = Template->new( INCLUDE_PATH => '/path/to/templates' );

   # define some data
   my $data = {
      hosts => [ 'host1', 'host2', 'host3', ...etc... ],

   $tt->process('index.html', $data) || die $tt->error();

Then in the index.html template (in the /path/to/templates directory) you
would write something like this:

   [% INCLUDE header %]

   [% FOREACH host IN hosts %]
       <td style="vertical-align: top;">[% host %]</td>
   [% END %]

   [% INCLUDE footer %]

Your header/footer templates would also need to be in the /path/to/templates

See http://tt2.org for more info on TT.  See the 'tpage' and 'ttree' scripts
(distributed with TT) for processing templates from the command line (or cron


More information about the Thamesvalley-pm mailing list