cleverett has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking to implement a bunch of pages with Apache::Template. I'd just as soon not reinvent the wheel/learn things the hard way/get things wrong the first time.

What I don't like about Apache::Template is that I don't see any facility for executing code before processing the template so that I can supply objects as parameters. Otherwise it seems to me that I arrive in the position of running perl code inside a template someplace, and I wish very much to avoid that ...

I've googled exhaustively and hunted around perlmonks and perl.com for a few hous and I really can't find anything at all useful.

can anyone supply some broad advice on how to use Apache::Template so I'm just pulling values from objects and using the mini-language just for display logic?

Replies are listed 'Best First'.
•Re: Is there a Apache::Template HOWTO
by merlyn (Sage) on Jul 23, 2003 at 05:54 UTC
      > I did that by setting a "TT2ServiceModule" that subclassed
      > Template::Service::Apache and overrode params to add my
      > additional objects and coderefs to the global namespace.

      For subclassing T::S::A then, I see for the source where he put the built in hook already to do something like:

      package My::Template::Service; use base qw/Template::Service::Apache/; sub param { my ($self, $request, $params) = @_; $params = $self->SUPER::params($request, $params); return { $self->custom_params($request, $params), %$params } if $self->can("custom_params"); return $params; }
      Then I write subclasses of that:
      package MedBanner::Account::CRUD; use base qw/My::Template::Service/; sub custom_params { my ($self, $request, $params); ## ... }
      Then all I have to do is use the TT2ServiceModule directive to make sure it gets used.

      This is exactly what I was looking for.

      Thanks again, Merlyn.

        I don't like the use of can there. Why not a simple callback with override:
        sub param { my ($self, $request, $params) = @_; $params = $self->SUPER::params($request, $params); return { $self->custom_params($request, $params), %$params }; } sub custom_params { # override this in subclasses to return a list of additional # key/value pairs to be added to your global namespace return (); }

        It simplifies the code tremendously, and it's also much faster.

        Introspection is expensive. Avoid it when possible.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Is there a Apache::Template HOWTO
by perrin (Chancellor) on Jul 23, 2003 at 14:51 UTC
    Keep in mind that there's no reason you need to use Apache::Template. Calling Template Toolkit from a mod_perl handler only takes a few lines of code. You can find examples in the tutorials that come with TT.

      I second that suggestion. I find it is often simpler to write a custom handler, rather that use something based on A::T.

      Sometimes it's easier to produce something simple and specific rather than trying to shoehorn your code into a generic framework.