The following is a method for Catalyst sites using Template Toolkit with a site-wide WRAPPER directive to selectively return partial HTML for the purpose of serving Asynchronous HTML and HTTP (ie, “AHAH’).
Catalyst is a powerful and versatile web development framework that aims to be Perl’s counterpart to Ruby on Rails, fusing a Model-View-Controller (MVC) environment with the depth, breadth, and power of CPAN. If you build web sites with Perl, you should check it out.
The problem: you want to use TT’s WRAPPER directive to supply a site-wide wrapper template, but you also want to be able to selectively turn the wrapper off so that you can serve some pages as partial HTML. At first glance, Catalyst’s config() method might suggest that one could reconfigure your view in your action’s end(), but that proves fruitless and it clucks in the following manner:
[warn] Setting config after setup has been run is not a good idea.
My solution: create an end()
in your controller that emits “partial” HTML fragments by creating a new Template Toolkit object with a locally-defined configuration. (Note that since we override the root controller’s end()
, we must call forward/detach to it if we do not specify the “partial” parameter.)
sub end : Private {
my ($self, $c, $file) = @_;
my $params = $c->request->parameters;
if (exists $params->{'partial'}) {
my $html;
my $tt = Template->new({
%{ $c->config->{'View::TT'} },
INCLUDE_PATH => $c->path_to('root'),
WRAPPER => undef,
});
$tt->process($file, $c->stash, \$html) or $c->log->warn($tt->error);
$c->response->content_type('text/html');
$c->response->body($html);
}
else {
$c->detach(qw/CE::Controller::Root end/);
}
}
A more robust solution better-suited for larger projects might be a new view with an alternate configuration (eg, a custom View::TTpartial,) but I wanted to show the logic directly in my controller, allowing full control of the configuration and direct access to the $html.
Coming soon: the “data island” solution I ultimately opted for to obviate AHAH for serving map marker data to a Google map.