Jeff Till wrote:
> What advantages does TT offer beyond what perl already provides?
As others have already pointed out, there's technically nothing that TT
can do that Perl can't, given that TT is compiled to Perl code.
But there are things that TT makes easier...
> For example, we already have
> functions in a perl library called header() and footer() that will print out
> those components of the pages.
The key benefit is that TT gives you an environment and language syntax
which is better suited to programming presentation elements and doing
data formatting than Perl.
Consider the difference between this in Perl:
print "<td><b>Name</b></td>\n";
print "<td>$user->{name}</td>\n";
and the TT equivalent
<td><b>Name</b></td>
<td>[% user.name %]</td>
The latter is, to my eye at least, easier to read and understand.
The document content is passed through as is, with the exception
of the [% ... %] directives, of course. There's no need for
print statements, quotes, escaping, \n newlines and so on.
We're saying in the [% user.name %] directive that the user name is
inserted here, but don't know or care if 'user' is a hash, in which
case TT will insert the 'name' key, or if 'user' is an object, in
which case TT will call the 'name()' method. They are implementation
details which should quite rightly be left up to the application
layer, i.e. the back-end Perl code.
This kind of abstraction, or to put it another way, this separation of
concerns between the application programming layer and the presentation
layer, is a Good Thing. It allows you to change your implementation
without affecting the presentation templates.
Other useful features of TT, to me at least, are things like filters,
wrappers, plugins, iterators and virtual methods. They generally
don't provide anything sensational in terms of functionality, but
they're useful and generally easy to use with the minimum of syntax.
> Toolkit outweigh the training and conversion time that would be needed to
> incorporate TT?
I'm pretty sure you could start using TT from scratch in no time.
The various modules, options, plugins, documentation, and so on are
all a little daunting, but in the end it's pretty straightforward.
Perhaps you could start by replacing a few of your existing header()
and footer() sub-routines with a call to process a TT template
instead?
my $tt = Template->new({ INCLUDE_PATH => '/wherever' });
sub header {
my $args = { @_ };
$tt->process( header => $args )
|| die $tt->error();
}
Then you would call it as:
header( title => 'The Cat Sat on the Mat' );
and have a 'header' template defined in your INCLUDE_PATH that
looked something like this:
<html>
blah blah
<title>[% title %]</title>
...etc...
That's really all you need to get started. As and when you need
to do more complicated things, you can start exploring some of the
other directives. But even if you do nothing more than reference
simple variables and INCLUDE some other templates, you should still
find it easier to manage your HTML as template than in Perl code,
I'm sure.
Hope this helps.
A
_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates