Hi,

I'm a Template-Toolkit newbie/possible convert.  I'm  researching the
best way to integrate TT and Class::DBI because I'm attracted to the
concept of filling templates by pulling data from objects.

I've gone down the path of querying a template for the variables it
wants and filling a hash to pass back to the template with another
template system, but that's a massive kludge.

However, I also need to avoid putting application logic in templates
since someone else does the HTML, and they're a PHP level person at
best.

I looked at Apache::Template, but AFAICT, it's not quite what I want.

Firstly, configuration is static, I'd prefer configuring via static
text files.

Second, (and correct me if I'm wrong on this), configuration is global
across entire servers.  I want my httpd.conf to look something like:

   PerlSetVar   TT2ConfigNames    Foo:Bar:Baz
   PerlSetVar   FooTT2ConfigFile  /var/httpd/apps/foo/config
   PerlSetVar   BarTT2ConfigFile  /var/httpd/apps/bar/config
   PerlSetVar   BazTT2ConfigFile  /var/httpd/apps/baz/config

   <Location /foo>
     SetHandler   perl-script
     PerlSetVar   TT2Config   Foo
     PerlHandler  Apache::Template::DynamicConfig
   </Location>

   # as above for Bar and Baz

And then use Config::ApacheFormat on the *TT2ConfigFile:

   PerlSetVar TemplateRoot   /var/httpd/apps/foo/templates

   <Location /foo/quux>
     PerlSetVar InputHandler   Foo::Quux
   </Location>

   # as above for any other locations

I looked at the Apache::Template docs and source and it appears
that subclassing Template::Service::Apache would be a good start.

But then it appears that since I'm moving the configuration directives
into a different file I could ditch Apache::Template and TSA entirely
and just grab the configuration (out of cache if possible), like this:

package My::Template;

use strict;
use My::Template::Config;  # custom, caches configs
use Template;

sub handler {
    my $r = shift;

    ## get the configuration
    my $config = My::Template::Config->new($r);

    ## do processing not related to generating output
    my %params = map { $_->process($r) } $config->process_with;
    $r->send_http_headers();    # headers set by processors
    return OK if $r->head_only or $r->status||OK != OK;

    ## now generate output
    my $template = Template->new($config->tt2_config);
    $template->process($config->template_file, \%params, \$output) or do
        $r->log_reason($SERVICE->error(), $r->filename());
        return SERVER_ERROR;
    }
    print $output;
    return OK;
}

Any thoughts before I go off the deep end?

Reply via email to