[Catalyst] Performance

Jason Kohles email at jasonkohles.com
Mon Mar 12 14:03:51 GMT 2007


On Mar 9, 2007, at 10:04 AM, Robert 'phaylon' Sedlacek wrote:

> Christopher H. Laco wrote:
>
>> Sure, it they're that different. The goal still stands, don't use
>> uri_for everywhere. Only use it when you really need it.
>
> Jep. But this is not getting easier if you start to have captures  
> in your chains. I'm still having high hopes to build something fast 
> (er) with URI::Template.
>
I actually built something to do this just this weekend, because I  
had some big tables that were calling uri_for more than once for  
every row of the table.  I ended up doing something like this...

In the controller...

$c->stash->{ 'uri_templates' }->{ 'id' } = $c->req->uri_with 
( { object_id => '[*id*]' } )->as_string;

In the template....

[% WHILE ( row = resultset.next ) %]
     [% FOR col IN columns %]
         [% IF uri_templates.$col %]
             [% action_link( uri_templates.$col, row ) %]
         [% ELSE %]
             [% row.$col %]
         [% END %]
     [% END %]
[% END %]

Elsewhere...

sub action_link {
     my ( $template, $object ) = @_;

     while ( $template =~ /\[\*(\w+)\*\]/ ) {
         my $i = $1;
         my $rep = $object->$i();
         $template =~ s/\[\*$i\*\]/$rep/g;
     }
     return $template;
}

I actually looked at URI::Template, and the one major issue I had  
with it was the choice of { and } as the template indicator  
characters, you can't do $c->req->uri_with( { foo => '{bar}' } ),  
since URI will escape the { and }, and your template will end up  
saying %7Bbar%7D instead of {bar}.

-- 
Jason Kohles
email at jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070312/266077cf/attachment.htm


More information about the Catalyst mailing list