[Templates] Template with mod_perl2

Perrin Harkins perrin@elem.com
Tue, 16 Jan 2007 00:52:43 -0500


michael higgins wrote:
> I was hoping you could expand a bit on your answers...

Sure.  Let me see if I can break it down for you, and give you some 
references for further reading.

First, there's TT.  It's a templating tool that takes a template and 
some data and merges them.  It doesn't care what you use it for (HTML, 
e-mail, PDF, reports, whatever) or where you call it from (mod_perl, 
classic CGI, command-line script, whatever).

Then there's mod_perl, which lets you write web applications that run in 
an embedded Perl interpreter inside apache.

You can stop there, write a little ModPerl::Registry script, call TT 
from it to generate some HTML, and you're done.  Congratulations on 
integrating TT and mod_perl 2.  And I will point to you some example 
code in a minute.

Or, you could run a framework like CGI::Application, Catalyst, or 
Maypole, which all run on mod_perl and have some helper code to make it 
even easier to call TT.

Or, you can run Apache::Template.  It's a little glue code between 
mod_perl and TT which looks at the file the current web request would 
map to and just runs that through TT, with no data.  I'm 
over-simplifying it quite a bit here, but that's the gist.

You can use Apache::Template to run your templates if all you want is a 
mostly static site with some includes and such.  You can also use it if 
you want to do all your programming through TT plugins, and modules 
which you call from TT templates.  There's a DBI plugin, and you can use 
most modules that provide an OO interface in this way without needing to 
make changes.  However, you'll be doing a lot of coding in TT's 
language, rather than in perl, which is a bad idea for anything complicated.

There's also a way to make Apache::Template run some code to generate 
data before running your template, but I think most people would find it 
easier to write a mod_perl script or handler that does the work and then 
calls TT.

> if I
> understand you correctly, and I don't "need" anything to run TT via
> Apache... what is the reason for Apache::Template then?

Some people just wanted a small step up from server-side includes rather 
than a full perl programming environment.  For those people, a simple 
approach like Apache::Template can be convenient.  You can even do 
in-line perl with, PHP-style, if you really want to.  (There are lots of 
more full-featured options for doing that though, like Mason.)

> And if I want to write some code to talk to DBs & such, why would I
> avoid Apache::Template?

Maybe you prefer to do your real coding in perl rather than TT's 
language, and don't want to use TT plugins for the bulk of your program. 
  Maybe you like the idea of the model-view-controller (MVC) approach 
that is often discussed in web dev circles, where some code 
("controller") at the beginning chooses which template ("view") to run, 
rather than a template running the show.

> I ask because the difficulty of getting TT to work with MP2 and
> Apache2 seems huge.

No, not at all.  It only takes a few lines of code, and frameworks exist 
to make it easier for you if you like.  There's good TT integration from 
Catalyst and from CGI::Application, among others.

> Are you, in effect, saying that's a waste of
> time? If Apache2 with Modperl2 doesn't need Apache::Template, then
> why? Did Apache and Modperl ever need it? What's the difference?

I'm saying that Apache::Template was never needed to build mod_perl apps 
that call TT.  It is one option for doing so.

> I have a project I want to code in
> Maypole, that I'd intended to use in Apache and modperl. Version 2.
> Without a headache. Is this even a good idea at all, then?

What, Apache::Template?  It would be something you would use instead of 
Maypole, not in addition to it.  Maypole already knows how to call TT, 
as do the frameworks I mentioned above.

Here are some reading recommendations.

For examples of using TT from a mod_perl script or handler, check out 
the tutorial on the TT website:
http://www.template-toolkit.org/docs/leon/Tutorial/Web.html

For a "How does this all hook together?" introduction, see Kate Pugh's 
great perl.com article:
http://www.perl.com/pub/a/2003/07/15/nocode.html
The code she shows here will work from mod_perl 1 or 2, FastCGI, CGI, etc.

If you'd like a more substantial read that goes into more depth, I 
recommend the book O'Reilly published about TT:
http://www.oreilly.com/catalog/perltt/index.html

- Perrin