Providing SSI-like includes for TT in Catalyst

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";
    }
}

< Pictures of Folkestone in the snow | How to make Vim syntax coloring work for TT templates >

Miniblog

(nuggets of inanity)

Tuesday Apr 24th 2007, 16:54 »
Just took the annual web design survey that AListApart do. I don't realy consider myself to be a web designer, but I have been doing a lot of HTML and CSS lately.
Monday Apr 23rd 2007, 18:23 »
Strange, there appears to be a bare-knuckle boxing match going on in the field outside my flat. Wish they wouldn't make so much noise about it.
Thursday Mar 1st 2007, 18:47 »
“In its written form, Hebrew has no vowels, making it the ideal language for texting.”
—Said in jest on some Radio 4 programme just now.

Archive: 2007 · 2006 · 2005 · 2004
Feed