Like Dan already mentioned it doesn't matter where images are (besides the cgi-bin) so long as you use a absolute path instead of a relative path. I always put my templates above the doc root and just set the path in H::T accordingly. If you have templates in more than one path H::T will take an arrayref to the path arg to accommodate. Here is something close to what I do. I use CGI::Application so it's not exact but it gives you an idea:
my ($self, $tfile, $args) = @_;
# default param set for the templates
$args ||= {};
# Create our template object
require HTML::Template;
my $t = HTML::Template->new(filename => $tfile,
# Add all your paths here
path => ['/path/to/templates',
'/alternate/path'],
# Associate on objects. I usually associate
# my CGI object by default. ymmv.
#associate => $self->query,
# The usual defaults here:
die_on_bad_params => 0,
loop_context_vars => 1,
global_vars => 1,
);
$t->param(%$args);
return $t;
}
# Then when you need a template object:
my $t = $self->load_tmpl('my_file.tmpl', { some => 'more args here'});
print $t->output;
# And if the template is in a subdir of one of your paths:
my $t = $self->load_tmpl('sub_dir/my_file.tmpl', { some => 'more args here'});
print $t->output;
# or even shorter:
print $self->load_tmpl('my_file.tmpl', { some => 'more args here'})->output;
~Charlie
Code:
sub load_tmpl { my ($self, $tfile, $args) = @_;
# default param set for the templates
$args ||= {};
# Create our template object
require HTML::Template;
my $t = HTML::Template->new(filename => $tfile,
# Add all your paths here
path => ['/path/to/templates',
'/alternate/path'],
# Associate on objects. I usually associate
# my CGI object by default. ymmv.
#associate => $self->query,
# The usual defaults here:
die_on_bad_params => 0,
loop_context_vars => 1,
global_vars => 1,
);
$t->param(%$args);
return $t;
}
# Then when you need a template object:
my $t = $self->load_tmpl('my_file.tmpl', { some => 'more args here'});
print $t->output;
# And if the template is in a subdir of one of your paths:
my $t = $self->load_tmpl('sub_dir/my_file.tmpl', { some => 'more args here'});
print $t->output;
# or even shorter:
print $self->load_tmpl('my_file.tmpl', { some => 'more args here'})->output;
~Charlie