Perl Programming Page 2 - More Templating Tools for Perl |
While the solutions we've seen so far have been primarily for Perl programmers--embedding Perl code in some other medium--Andy Wardley's Template Toolkit (http://www.template-toolkit.org/) is slightly different. It uses its own templating language to express components, loops, method calls, data structure elements, and more; it's therefore useful for teaching to designers who have no knowledge of the Perl side of your application* but who need to work on the presentation. As the documentation puts it, you should think of the Template Toolkit language as a set of layout directives for displaying data, not calculating it. Like Mason, it seamlessly handles compiling, caching, and delivering your templates. However, unlike Mason, it's designed to provide general-purpose display and formatting capabilities in a very extensible way. As an example, you can use Template Toolkit to dynamically serve up PDF documents containing graphs based on data from a database--and all this using nothing other than the standard plugins and filters and all within the Template Toolkit mini language. But before we look at the clever stuff, let's look at the very simple uses of Template Toolkit. In the simplest cases, it behaves a lot like Text::Template. We take a template object, feed it some values, and give it a template to process: use Template; who => "Andy Wardley", This time, our template looks like the following: Dear [% who %], Love, Lo and behold, the templated text appears on standard output. Notice, however, that our variables inside the [% and %] delimiters aren't Perl variables with the usual type sign in front of them; instead, they're now Template Toolkit variables. Template Toolkit variables can be more than just simple scalars, though; complex data structures and even Perl objects are available to Template Toolkit through a simple, consistent syntax. Let's go back to our design work invoices, but with a slightly different data structure: my $invoice = { $invoice->{total} += $_->{cost} for @{$invoice->{jobs}}; How would we design a template to fit that data? Obviously, we're going to need to loop over the jobs in the anonymous array and extract various hash values. Here's how it's done: To [% client %]: Thank you for consulting the services of Fungly Foobar Design [% FOREACH job = jobs %] Total $[% total %] Payment terms 30 days. Many thanks, As you can see, the syntax is inspired by Perl--we can foreach over a list and use a local variable job to represent each element of the iterator. The dot operator is equivalent to Perl's ->-- it dereferences array and hash reference elements and can also call methods on objects. However, there's something slightly wrong with this example; since we can expect our descriptions to be of variable width, our costs aren't going to line up nicely at the end.* What can we do about this? This is where a nice, extensible feature of the Template Toolkit called filters comes in.
blog comments powered by Disqus |
|
|
|
|
|
|
|