Home arrow Perl Programming arrow Page 2 - Getting Started with the Perl Template Toolkit

The Templating Ecosystem - Perl

This chapter compares the Template Toolkit to other templating systems such as HTML::Mason and HTML::Template, describes how to install it, then shows you what templates look like and how to process them. (From the book, Perl Template Toolkit, O'Reilly Media, ISBN: 0596004761, by Darren Chamberlain, Dave Cross and Andy Wardley.)

TABLE OF CONTENTS:
  1. Getting Started with the Perl Template Toolkit
  2. The Templating Ecosystem
  3. Installing the Template Toolkit
  4. Documentation and Support
  5. Using the Template Toolkit
  6. The Template Module
  7. The Template Toolkit Language
  8. Dynamic Variables
  9. Template Processing Directives
  10. Integrating and Extending the Template Toolkit
By: O'Reilly Media
Rating: starstarstarstarstar / 23
July 28, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

At least half a dozen mature and respected templating systems are available for Perl. The best-known and best-supported template processors include the following:

Text::Template

Text::Template is a library for generating form letters, building HTML pages, or filling in templates generally. A template is a piece of text that has little Perl programs embedded in it here and there. When you fill in a template, you evaluate the little programs and replace them with their values. These programs are written in Perl: you embed Perl code in your template, with { at the beginning and } at the end. If you want a variable interpolated, you write it the way you would inPerl. If you need to make a loop, you can use any of the Perl loop constructions.

All the Perl built-in functions are available.

Text::Template is available from http://www.plover.com/~mjd/perl/Template/ or from CPAN (http://search.cpan.org/dist/Text-Template/).

HTML::Template

HTML::Template attempts to make using HTML templates easy and natural. It extends standard HTML with a few HTML-like tags, and enforces the divide between design and programming by restricting what a template is capable of doing. By limiting the programmer to using just simple variables and loops in the HTML, the template remains accessible to designers and other non-Perl people. The use of HTML-like syntax goes further to make the format understandable to others.

HTML::Template is available from CPAN (http://search.cpan.org/dist/HTML-Template/).

HTML::Mason

HTML::Mason is a Perl-based web site development and delivery system. Mason allows web pages and sites to be constructed from shared, reusable building blocks called components. Components contain a mix of Perl and HTML, and can call each other and pass values back and forth like subroutines. Components increase modularity and eliminate repetitive work: common design elements (headers, footers, menus, logos) can be extracted into their own components where they need be changed only once to affect the whole site. Mason also includes powerful filtering and templating facilities and an HTML/ data caching model.

HTML::Mason is available from http://www.masonhq.com/ and CPAN (http://search.cpan.org/dist/HTML-Mason/).

HTML::Embperl

Embperl gives you the power to embed Perl code in your HTML documents, and the ability to build your web site out of small reusable objects in an object-oriented style. You can also take advantage of all the usual Perl modules (including DBI for database access), use their functionality, and easily include their output in your web pages.

Embperl has several features that are especially useful for creating HTML, including dynamic tables, form field processing, URL escaping/unescaping, session handling, and more.

Embperl is a server-side tool, which means that itís browser-independent. It can run in various ways: under mod_perl, as a CGI script, or offline.

HTML::Embperl is available from http://www.ecos.de/ or CPAN (http://search.cpan.org/dist/HTML-Embperl/).

Apache::ASP

Apache::ASP provides an Active Server Pages port to the Apache web server with Perl scripting only, and enables development of dynamic web applications with session management and embedded Perl code. Apache::ASP also provides many powerful extensions, including XML taglibs, XSLT rendering, and new events not originally part of the ASP API.

Apache::ASP is available from CPAN (http://search.cpan.org/dist/Apache-ASP/).

The Template Toolkit attempts to offer the best features of these modules, including separation of Perl from templates and applicability beyond HTML.

The Template Toolkit Is for More Than HTML

The Template Toolkit is a generic template processing system that will process any kind of document for use in any environment or application. Many other template systems were designed specifically to create HTML pages for web content. In some cases, that is all the system can be used for. In others, it is possible (with varying degrees of difficulty) to use the system in a non-web environment.

The Template Toolkit was originally designed to help Andy create his web site, but he was careful to ensure that it was just as usable outside of that environment. As a result, there is nothing within the Template Toolkit that assumes it is being used to generate HTML. It is equally at home creating any other kind of data.

The Template Toolkit Lets You Choose Your Separation

Template Toolkit doesnít prescribe any particular methodology or framework that forces you to use it in a certain way. Some modules (for example, HTML::Template) enforce a very strict interpretation of template processing that intentionally limits what can be done in a template to accessing variables and using simple conditional or looping constructs. Others (such as HTML::Mason and HTML::Embperl) use embedded Perl code to allow any kind of application functionality to be incorporated directly into the templates.

The Template Toolkit gives you the best of both worlds. It has a powerful data engine (the Stash) that does all the hard work of mapping complex data structures from your Perl code, configuration files, SQL databases, XML files, and so on, into template variables that are accessed by a simple and uniform dotted notation (e.g., person.surname). You can use this to keep your templates simple without limiting the complexity or functionality of the systems that put data into the templates.

At the opposite end of the spectrum, the Template Toolkit also allows you to embed Perl code directly in your templates. We donít normally encourage this because it tends to defeat the purpose of having a template processing system in the first place. Because this is the exception rather than the norm, template processors must set the EVAL_PERL option to embed Perl code in the template (it is disabled by default). We look at how to set options later in this chapter.

Template Toolkit also lets you work between the two extremes. It provides a rich set of language features ( directives) that allow you to add complex functionality to your templates without requiring you to embed Perl code. It also has a powerful plugin mechanism that allows you to load and use Perl modules to extend the functionality in any way you can imagine.

In short, the Template Toolkit allows you to take a modular approach to building your web site or other document system, but doesnít enforce it. Sometimes you want to build a complex and highly structured system to run a web site. Other times you just want to roll up a quick all-in-one template to generate a report from a database. The Template Toolkit encourages whatever approach is most appropriate to the task at hand.

Nonprogrammers Can Maintain Templates

Template Toolkitís template language is designed to be as simple as possible without being too simple. The dotted notation makes accessing variables far less daunting than in Perl. For example:

$person->{surname}    # Perl
person.surname        # Template Toolkit

This hides the underlying implementation details from the template designer. In the previous example, the Perl syntax implies that $person is a reference to a hash array containing a surname value. However, you might one day decide to implement $person as an object with a surname( ) method:

$person->surname() # Perl
person.surname     # Template Toolkit

The Perl code requires a different syntax but the Template Toolkit code stays the same. This lets you change the underlying implementation at any time without having to change the templates. As long as the data is laid out in the same way (i.e., donít change surname to last_name), it doesnít really matter what data structures are used, or whether they are precomputed, fetched from a database, or generated on demand.

This uniform syntax also means that your template designers can remain blissfully ignorant of the difference between a hash array and an object. They donít have to worry about any confusing syntax and can concentrate on the task at hand of presenting the data nicely. This makes the template language as friendly as possible for people who arenít already Perl programmers.

The general rule is to use Perl for programming and the Template Toolkit for presentation. But again, itís not mandatory, so youíre still free to bend (or break) the rules when you really need to.

The Template Toolkit Is Easy to Extend

The Template Toolkit is designed to be easy to extend. If it doesnít already do what you want, thereís a good chance you can reimplement a small part of it to change it to do what you what. The object-oriented architecture of the Template Toolkit makes this process relatively straightforward, and there are programming hooks throughout the system to give you as much flexibility as possible.

A number of plugins exist for the Template Toolkit, and we cover them in Chapter 6. They are designed to give templates convenient control over things such as HTML tables, database connections, and CGI parameters.  

Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

Visit the O'Reilly Network http://www.oreillynet.com for more online content.



 
 
>>> More Perl Programming Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: