<div class="gmail_quote">On Wed, Sep 2, 2009 at 6:25 PM, Trevor Phillips <span dir="ltr"><<a href="mailto:trevor.phillips@gmail.com">trevor.phillips@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I have a requirement to fetch URL content - this is currently mostly<br>
to do with the View phase, to include chunks of HTML for core branding<br>
in the templating. ie; Top header, footer, global nav, that sort of<br>
thing.<br>
<br>
Ideally, I'd like to do it directly from the Template Toolkit<br>
templates, but TT either can't do this, or it's really hard to Google<br>
for. ^_^<br>
It would be nice to cache locally the content, even if for a short<br>
period. Or ideally, to cache permanently unless there's a<br>
"Cache-Control: no-cache" request header to force an update.<br>
<br>
ie; something like:<br>
[% INCLUDE "<a href="http://central.server.com/branding/header.html" target="_blank">http://central.server.com/branding/header.html</a>" cache="5 min" %]<br>
<br>
Can TT do anything like this? It doesn't need to be parsed by TT<br>
(although having the option for TT to parse it would be useful).<br>
<br>
The next idea would be to have a Controller fetch the content for the<br>
remote URLs, and either shove them in the stash, or cache them to<br>
disk, so that TT can then reference them. Would this just be a case of<br>
doing a usual LWP UserAgent Request, or does Catalyst have built-in<br>
handling or plugins to aid in this?<br>
<br>
Hmmm. It could be implemented as a Model as well, I guess...<br>
<br>
Any ideas/tips/best practices?<br>
<br>
Thanks.<br>
<br>
</blockquote><div><br>Putting it into the template definitely falls under the bad idea category. Having something like this as a plugin is also a very bad idea.<br><br>While you could, very easily, create this behavior by extending View::TT it is better to put this in a very simple model class. The caching would be very easy as well.<br>
<br>You could then just wrap one of the LWP::UserAgent and be done. I'm currently doing something similar with Web::Scraper, and it works quite well.<br><br>This is a non-tested/typed up example of what I have (I don't remember exactly which app has it :/). This is just an example of a thin model that can be used to accomplish mostly what you want, not really for you to just copy and paste in. You can add in caching as necessary.<br>
<br>package MyApp::Model::Scraper;<br><br>use Moose;<br>use Web::Scraper;<br><br>BEGIN { extends "Catalyst::Model"; }<br><br>has 'scraper' => {<br> is => 'rw',<br> isa => 'Web::Scraper',<br>
default => sub {<br> scraper { ... }<br> }<br>}; <br><br>sub fetch {<br> my ( $self, $url ) = @_;<br><br> return $self->scraper->scrape( URI->new( $url ) );<br>}<br><br>no Moose;<br>__PACKAGE__->meta->make_immutable;<br>
<br></div></div>