<div class="gmail_quote">On Wed, Sep 2, 2009 at 6:25 PM, Trevor Phillips <span dir="ltr">&lt;<a href="mailto:trevor.phillips@gmail.com">trevor.phillips@gmail.com</a>&gt;</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&#39;d like to do it directly from the Template Toolkit<br>
templates, but TT either can&#39;t do this, or it&#39;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&#39;s a<br>
&quot;Cache-Control: no-cache&quot; request header to force an update.<br>
<br>
ie; something like:<br>
   [% INCLUDE &quot;<a href="http://central.server.com/branding/header.html" target="_blank">http://central.server.com/branding/header.html</a>&quot; cache=&quot;5 min&quot; %]<br>
<br>
Can TT do anything like this? It doesn&#39;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&#39;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&#39;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 &quot;Catalyst::Model&quot;; }<br><br>has &#39;scraper&#39; =&gt; {<br>    is =&gt; &#39;rw&#39;,<br>    isa =&gt; &#39;Web::Scraper&#39;,<br>
    default =&gt; sub {<br>        scraper { ... }<br>    }<br>}; <br><br>sub fetch {<br>    my ( $self, $url ) = @_;<br><br>    return $self-&gt;scraper-&gt;scrape( URI-&gt;new( $url ) );<br>}<br><br>no Moose;<br>__PACKAGE__-&gt;meta-&gt;make_immutable;<br>
<br></div></div>