APM: Looking for a good HTML templating system

Mike South msouth at gmail.com
Thu Oct 27 09:41:31 PDT 2005


I second the sentiment below about HTML::Template limitations, but I
would go Template Toolkit rather than Mason.  It's easier to do very
lightweight things in Toolkit (in my opinion--maybe my Mason
experience is too little?).  Bascially, I would put it like this:

HTML::Template has a very small learning curve but will very likely
run out of steam if your application grows

Template Toolkit's learning curve is not much worse than
HTML::Template's, and in fact it will let you write almost exactly the
same template code as HTML::Template, but it will probably never run
out of steam no matter what you decide you want it to do if your needs
grow more complex.

Mason will also let you do very flexible and powerful things like
Toolkit, but it's harder to get up and running on it quickly.

Here is an all-in-one-file (the template is stored at the end of the
file in the __DATA__ section) script that shows you some of what
Toolkit can do, some of which could not be easily done in
HTML::Template (I posted this to the list a while back when there was
a similar question):

#!/usr/bin/perl
use strict;
use Template;

my $folks = [
    {
        name => 'bob',
        fruit =>'kiwi',
        friends => [qw/ sally jessie raphael/],
    },
    {
        name => 'sally',
        fruit => 'kumquat',
        friends =>[],
    },
    {
        name => 'misery',
        fruit => 'company',
        friends =>[qw/ avarice sloth /],
    },
    {
        name => 'batman',
        fruit => 'robin',
        friends =>[qw/ catwoman /],
    },
];

#and you pass that in to a template as 'folks',  you can have a loop like this:

my $template = Template->new();

my $output;

$template->process(\*DATA, {folks=>$folks}, \$output) || die $template->error;

print $output;

__DATA__
[%- FOREACH person = folks %]
======================

    [%- person.name %] likes [% person.fruit %]
        [%- FOREACH friend = person.friends -%]
            [%- IF loop.first %]
[% person.name %] is a friend of:[% END -%]
                [%- IF loop.last && person.friends.size > 1  %]
and[%END %] [% friend %][% IF not loop.last and person.friends.size >
2%],[% END -%]
        [%- END -%]
    [%- IF loop.last  %]
======================
    [%- END -%]

[%- END  %]

This prints

======================
bob likes kiwi
bob is a friend of: sally, jessie, and raphael
======================
sally likes kumquat
======================
misery likes company
misery is a friend of: avarice and sloth
======================
batman likes robin
batman is a friend of: catwoman
======================


On 10/27/05, Brent LaVelle <brentlavelle at yahoo.com> wrote:
> Warning: dated material
>
> I started using HTML::Template a while ago for simple projects like
> http://portfolio.cunninghamarchitects.com/cgi-bin/projects found it to
> be annoying.  A good example is that inside a loop if I wanted to
> access variable outside the loop I had put them on each hash element I
> was looping over.  I got real sick of HTML::Template fast.
> Also think of tables where rowspan and colspan are >1.
>
> However the thing I went onto you already do not want to use.  I like
> Mason because of the content wrapping concept.  Wrapping things like
> headers and footers around content, I found, is so much better than
> including stuff.  Mason is a different way of thinking but very
> powerful.  The idea of the user just hitting a page that is nothing
> more than a SQL statment that is then wrapped and wrapped until it
> makes a report is very powerful.
>
> So, I'd like to bring HTML::Template down a notch and tell you to
> possibly rethink the Mason solution.  I suppose I would have been a
> real help if I had used something else.
>
> --- Bob Apthorpe <apthorpe+pm at cynistar.net> wrote:
>
> > Hi,
> >
> > It's been a while since I've done any CGI programming in perl but
> > recently I took on a project where we're using HTML::Template to
> > render
> > pages. My preference is to use a templating system to separate the
> > logic
> > from the presentation and most recently I've been using PHP and the
> > Smarty template system for web application development. Smarty has
> > its
> > quirks but it's easy enough to pass complex data structures into a
> > template to generate rather elaborate pages.
> >
> > Not having any previous experience with HTML::Template aside from
> > perldoc and some trivial examples, how do the two tools compare? Is
> > there a better system to use that does not require mod_perl? I'm
> > specifically eliminating HTML::Mason because it's too heavyweight for
> > what I'm trying to do, i.e. I'm looking more for a templating system
> > than an application framework (I'm still learning Rails...)
> >
> > The application is not expected to be high traffic so it can run as a
> > CGI without worrying much about performance. Conversely, this is for
> > a
> > client and I would rather not have to specify a highly-tuned
> > ("hacked")
> > Apache configuration, which is why I don't want to mess with
> > mod_perl.
> >
> > Google turns up
> > http://perl.apache.org/docs/tutorials/tmpl/comparison/comparison.html
> > and after reading that, I think Template Toolkit bears some
> > investigation. Still, I'd appreciate hearing people's experiences
> > with
> > perl templating systems.
> >
> > Thanks,
> >
> > -- Bob
> > _______________________________________________
> > Austin mailing list
> > Austin at pm.org
> > http://mail.pm.org/mailman/listinfo/austin
> >
>
>
>
>
> __________________________________
> Yahoo! FareChase: Search multiple travel sites in one click.
> http://farechase.yahoo.com
> _______________________________________________
> Austin mailing list
> Austin at pm.org
> http://mail.pm.org/mailman/listinfo/austin
>


More information about the Austin mailing list