in reply to Is there a Apache::Template HOWTO

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.

You could also do it by setting a "TT2PreProcess" that calls a plugin to compute your objects.

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

  • Comment on •Re: Is there a Apache::Template HOWTO

Replies are listed 'Best First'.
Re: •Re: Is there a Apache::Template HOWTO
by cleverett (Friar) on Jul 23, 2003 at 06:34 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.

        > I don't like the use of can there. Why not a simple
        > callback with override:

        <snip>

        Ah, good point.

        > Introspection is expensive. Avoid it when possible.

        Out of the mouths of monks :)