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

Hi People! I've been trying to "refine" my perl scripts so that there is no HTML tags, etc inside them and have been playing around with the HTML::Template module, but am totally confused at this point. My confusion lies in the following two areas -

The options that I see for keeping HTML and Perl scripts seperate are -

1. To use HTML::Template and have X number of template files i.e. one template for each page I want to show. My question here is - is this correct? Do I actually have to have multiple templates?

2. To spit out HTML gook from inside the script. With respect to the above method this looks easier as atleast my HTML is all in one area .... that is I dont have to modify 15-20 template files.

I guess what I'm trying to say is thT I don't see the benefit of using HTML::Template if I have X number of templates, where each has to be edited manually later.

I'm very confused .... need some guidance and knowledge.

Thanks, SP

Replies are listed 'Best First'.
Re: HTML::Template Question / Confusion
by pfaut (Priest) on Jan 11, 2003 at 23:51 UTC

    There is no reason to have a one-to-one relationship between templates and pages. I use different templates for each section of my pages. Use one for your standard header and footer and one for each main body portion. If your body can be broken down into multiple logical units, represent each in a separate template. (But I think this is heading in the other direction - this will create more templates, not fewer)

    The real advantage to HTML::Template is separation of form and function. Your code resides in your perl and is edited by a programmer. Your presentation resides in your template and is edited by a web designer.

    It's also easier to edit HTML in a template than editing CGI generated HTML. If you just want that amount of separation, you should be able to embed your HTML in your perl by placing it after the __DATA__ marker.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: HTML::Template Question / Confusion
by dws (Chancellor) on Jan 11, 2003 at 23:41 UTC
    Do I actually have to have multiple templates?

    No, you can include multiple templates within a single file using

    <TMPL_IF template1>...</TMPL_IF> <TMPL_IF template2>...</TMPL_IF> etc.
    but it isn't generally advisable. I keep mine separate template files.

    I guess what I'm trying to say is that I don't see the benefit of using HTML::Template if I have X number of templates, where each has to be edited manually later.

    Some might call that an advantage. If you have a lot of repetive code, <TMPL_INCLUDE> can ease the pain.

Re: HTML::Template Question / Confusion
by Aristotle (Chancellor) on Jan 11, 2003 at 23:50 UTC
    I would say what you really want to do is have a container.tmpl that defines the common header, footer, possibly navigation and so on (Once And Only Once principle), and then separate, probably very small templates for the pages to generate which are included into the container per a variable passed to it.

    Makeshifts last the longest.

      Aristotle,

      That actually makes sense to me .... I was wondering if you could explain a little more in this regard.

      Would greatly appreciate it.

      Thanks, Surya
        Just what is unclear? There's not much to explain I think, so I'm not sure what to clarify. What you'd do is basically something like this:
        # ... my @valid_page = qw(order cart shelf intro register); my %collected_data = (foo => 1, bar => 2, ); my $requested_page = $cgi->param('page'); my ($actual_page) = grep $requested_page eq $_, @valid_page; print("Content-type: text/plain\n\nNo valid page requested."), exit unless $actual_page; my $container = HTML::Template->new_file('container.tmpl'); my $template = HTML::Template->new_file($actual_page . '.tmpl'); $template->param(%collected_data); # fill in actual page $container->param( %collected_data, CONTENT => $template->output, # insert into container ); print "Content-type: text/html\n\n", $container->output; # we're done
        You would then have order.tmpl, cart.tmpl, shelf.tmpl, intro.tmpl, register.tmpl and container.tmpl in a directory, and the latter would have a <TMPL_VAR NAME="CONTENT"> somewhere, so that the other template's output is inserted at that point. If you need any pointers, tell me what you have trouble with.

        Makeshifts last the longest.

Re: HTML::Template Question / Confusion
by CountZero (Bishop) on Jan 12, 2003 at 12:53 UTC

    Ever thought of having your PERL-script spit out XML which then gets transformed into HTML or XHTML by XSLT?

    I find it the cleanest way to separate logic and form.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Question? How well does XSLT handle complex logic? I myself have been dabbling in the arts of XML/XSLT transformation, and it just seems:
      1. too slow (i anticiapte faster processing the future however)
      2. possibly incapable of handling very complex decisions (for example, lot's of nested if-else conditions and loops)
      3. only good when you need to transform XML to more than just HTML
      I have done quite a bit with XSLT, but nothing that involved complex decision trees. I am curious as to which is really the "cleanest way." (and so far ... i still don't get why TemplateToolkit is as popular as it is.)

      jeffa

      "PERL" ne "Perl"

        I have used XML and XSLT to present some insurance statistics. A PERL-script collects the statistical data out of the database and sends it out as XML. It then gets transformed to HTML in nice tables and graphs, sums and ratios are calculated, ... all by XSLT. I now rarely have to change the PERL-scripts and can do most of the work through the XSLT-scripts.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law