I've been developing a new site using the Perl Catalyst web development framework. One of the things I tried was including parts of a page design from separate files. On related websites we use SSIs to make it easier to alter the design without regenerating lots of static files. Now since the Catalyst site will be dynamic, we've decided just to use Template Toolkit includes for it instead. However, there might be times when you want to have included files processed by Apache separately before being inculded, or retrieved from a different server. In case I ever need it again, here's the code I used.
The goal is to allow things like this to be used in the TT templates:
<div class="Advert"> [% view.ssi('http://example.com/advert.html') %] </div>
I'm not sure if ssi is the best name for that method, but
it has the benefit of being short. The implementation goes in the
view class, for example MyApp/View/TT.pm:
use LWP::UserAgent; sub ssi { my ($self, $url) = @_; my $ua = LWP::UserAgent->new; my $response = $ua->get($url); if ($response->is_success) { return $response->content; } else { die "Error downloading URL $url: " . $response->status_line . "\n"; } }