I'm attempting to use mod_perl and Template Toolkit to serve up
templates. However, I'm having a problem with the images in those
templates: They're passing through the content handler, and thus getting
corrupted.
My first thought was to return DECLINED from the content handler if the
request is not for text/html content; however, since I'm using a
Location directive, the content_type is always empty since there's no
direct mapping to an actual image file. I could use an Alias to map the
URI to the file, but then I wouldn't have the path_info that I'm using
to call the template.
Since my test code, using path_info, is based on an example from the
Template Toolkit docs, I feel like I'm probably overlooking something
basic. So, I'd appreciate it if someone could show me the error of my
ways. :-)
Here are the relevant chunks of config and code:
from httpd.conf
---------------
...
<Location /tt>
SetHandler perl-script
PerlHandler Apache::Test::Mod
PerlSetVar WEBROOT /usr/local/apache/tt/html
</Location>
...
Apache::Test::Mod
-----------------
...
sub handler {
my $r = shift;
# this doesn't work
#return DECLINED unless $r->content_type eq 'text/html';
my $WEBROOT = $r->dir_config('WEBROOT')
or return fail( $r, SERVER_ERROR, "'WEBROOT' not specified" );
my $file = $r->path_info;
my $vars = {
content => $r->content_type,
};
$r->content_type('text/html');
$r->no_cache(1);
my $template = Template->new( {
INCLUDE_PATH => "$WEBROOT:$WEBROOT/include",
OUTPUT => $r,
} );
$template->process( $file, $vars, $r)
or return fail( $r, SERVER_ERROR, $template->error );
$r->send_http_header();
$r->print( $output );
return OK;
}
...
index.html (test template)
--------------------------
<html>
<head>
<title>test</title>
</head>
<body>
<p>content_type: [% content %]</p>
<p>image: <img src="images/hello.gif"></p>
</body>
</html>
Thanks.
-Tim