x
Loading
 Loading
Hello, Guest | Login | Register

Introduction to the Template Toolkit, Part One

In some of my past columns, I’ve mentioned that my template system of choice is the aptly named Template Toolkit, a marvelous work by Andy Wardley. Although I’ve demonstrated how I’ve used the Template Toolkit (TT), I haven’t really talked enough about what makes it so wonderfully useful. So, this month, let’s take a more in-depth look at the wonders of TT.

In some of my past columns, I’ve mentioned that my template system of choice is the aptly named Template Toolkit, a marvelous work by Andy Wardley. Although I’ve demonstrated how I’ve used the Template Toolkit (TT), I haven’t really talked enough about what makes it so wonderfully useful. So, this month, let’s take a more in-depth look at the wonders of TT.

Why Template Toolkit?

At a minimum, a templating system provides a way of replacing placeholders with values. The simplest templating system is a Perl “one-liner” such as this:

 my %v = (first => ‘Randal’, last => ‘Schwartz’); my $text = ‘My name is <first> <last>.’; ## here it comes: $text =~ s/<(w+)>/$v{$1}/g; 

Here, each word in angle brackets is replaced with a value found in the %v hash. Simple enough, and some might say deceptively simple. Because it’s this easy to code a trivial templating system, many people have started here and grown their own templating systems independently. But the tricky parts are indeed tricky. Eventually, templating systems grow to include features that make them approach full-blown languages.

But here’s where I think Andy did the right thing: TT’s control structures are handled by a mini-language. This mini-language carefully hides the differences between data structure access, method calls, and function calls, by sweeping them all under the same dot notation (just like Perl version 6). Thus, the TT mini-language is much more accessible to web designers who don’t care…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace