Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Fetching URL Content

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


trevor.phillips at gmail

Sep 2, 2009, 6:25 PM

Post #1 of 3 (824 views)
Permalink
Fetching URL Content

I have a requirement to fetch URL content - this is currently mostly
to do with the View phase, to include chunks of HTML for core branding
in the templating. ie; Top header, footer, global nav, that sort of
thing.

Ideally, I'd like to do it directly from the Template Toolkit
templates, but TT either can't do this, or it's really hard to Google
for. ^_^
It would be nice to cache locally the content, even if for a short
period. Or ideally, to cache permanently unless there's a
"Cache-Control: no-cache" request header to force an update.

ie; something like:
[% INCLUDE "http://central.server.com/branding/header.html" cache="5 min" %]

Can TT do anything like this? It doesn't need to be parsed by TT
(although having the option for TT to parse it would be useful).

The next idea would be to have a Controller fetch the content for the
remote URLs, and either shove them in the stash, or cache them to
disk, so that TT can then reference them. Would this just be a case of
doing a usual LWP UserAgent Request, or does Catalyst have built-in
handling or plugins to aid in this?

Hmmm. It could be implemented as a Model as well, I guess...

Any ideas/tips/best practices?

Thanks.

--
Trevor Phillips - http://dortamur.livejournal.com/
"On nights such as this, evil deeds are done. And good deeds, of
course. But mostly evil, on the whole."
-- (Terry Pratchett, Wyrd Sisters)

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


jshirley at gmail

Sep 2, 2009, 7:26 PM

Post #2 of 3 (748 views)
Permalink
Re: Fetching URL Content [In reply to]

On Wed, Sep 2, 2009 at 6:25 PM, Trevor Phillips
<trevor.phillips [at] gmail>wrote:

> I have a requirement to fetch URL content - this is currently mostly
> to do with the View phase, to include chunks of HTML for core branding
> in the templating. ie; Top header, footer, global nav, that sort of
> thing.
>
> Ideally, I'd like to do it directly from the Template Toolkit
> templates, but TT either can't do this, or it's really hard to Google
> for. ^_^
> It would be nice to cache locally the content, even if for a short
> period. Or ideally, to cache permanently unless there's a
> "Cache-Control: no-cache" request header to force an update.
>
> ie; something like:
> [% INCLUDE "http://central.server.com/branding/header.html" cache="5
> min" %]
>
> Can TT do anything like this? It doesn't need to be parsed by TT
> (although having the option for TT to parse it would be useful).
>
> The next idea would be to have a Controller fetch the content for the
> remote URLs, and either shove them in the stash, or cache them to
> disk, so that TT can then reference them. Would this just be a case of
> doing a usual LWP UserAgent Request, or does Catalyst have built-in
> handling or plugins to aid in this?
>
> Hmmm. It could be implemented as a Model as well, I guess...
>
> Any ideas/tips/best practices?
>
> Thanks.
>
>
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.

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.

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.

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.

package MyApp::Model::Scraper;

use Moose;
use Web::Scraper;

BEGIN { extends "Catalyst::Model"; }

has 'scraper' => {
is => 'rw',
isa => 'Web::Scraper',
default => sub {
scraper { ... }
}
};

sub fetch {
my ( $self, $url ) = @_;

return $self->scraper->scrape( URI->new( $url ) );
}

no Moose;
__PACKAGE__->meta->make_immutable;


moseley at hank

Sep 2, 2009, 9:46 PM

Post #3 of 3 (758 views)
Permalink
Re: Fetching URL Content [In reply to]

On Wed, Sep 2, 2009 at 6:25 PM, Trevor Phillips
<trevor.phillips [at] gmail>wrote:

> I have a requirement to fetch URL content - this is currently mostly
> to do with the View phase, to include chunks of HTML for core branding
> in the templating. ie; Top header, footer, global nav, that sort of
> thing.
>
> Ideally, I'd like to do it directly from the Template Toolkit
> templates, but TT either can't do this, or it's really hard to Google
> for. ^_^
>

http://search.cpan.org/~evdb/Template-Provider-HTTP-0.02/lib/Template/Provider/HTTP.pm

But I can't imagine *ever* using that. Use a cron job to update local
templates if you must.




--
Bill Moseley
moseley [at] hank

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.