[Catalyst] Loading template according to request path

Richard Thomas ret at mac.com
Thu Aug 13 00:58:13 GMT 2009


Matt,

I don't know if any of this is useful to you. We do something that  
sounds a bit like what you're after, by tweaking the template  
include_path with each request. It's a classic path setting algorithm,  
working back from most specific to least specific location.

ie, a template that says PROCESS "something.tt" that is the end result  
of the URI /myapp/xx/yy/zz will look for something.tt in tt/xx/yy/zz,  
then tt/xx/yy, then tt/xx, or tt/base. This gives us enormous  
flexibility to tweak the output at any node or leaf of the tree.

You can create empty something.tt files in various places too, that  
has a negating effect.

MyApp/C/Root.pm contains

sub auto {
     ...
     $c->stash->{reportfolders} = split('/',$c->req->path);
     ...
}

MyApp/V/TT.pm contains

sub process {
     my ($self, $c) = @_;
     if (my $reportfolders = $c->stash->{reportfolders}) {
         my @orig_path = @{$self->include_path};
         my @this_path = ();
         for my $orig_entry (@orig_path) {
             for (my @folders = @$reportfolders; @folders; pop  
@folders) {
                 push @this_path, join('/', $orig_entry, '..',  
@folders);
             }
             push @this_path, $orig_entry;
         }
         @{$self->include_path} = @this_path;
         #$c->log->debug(sprintf('%s:process: dynamic include_path is  
%s.', __PACKAGE__, join(':',@{$self->include_path})));
         $self->SUPER::process($c);
         @{$self->include_path} = @orig_path;
     } else {
         $self->SUPER::process($c);
     }
}

Hope that's useful.

cheers
RET
________________________________
Illegitimi non carborundum

On 12/08/2009, at 3:15 AM, Matt Whipple wrote:

>>
>
> Thanks everyone for your input, I'm certainly gathering that there  
> is nothing preexisting that does what I'm looking to do.  I'll  
> continue to clean up my version and throw it on CPAN if it seems  
> appealing enough.  As a quick overview the basic premise is a more  
> direct link from the path to the template which would be used for  
> those times when the URI path determines presentation after passing  
> through a reusable action handler which tailors content (and doesn't  
> necessarily have to worry about the template selection).  This is  
> presently done in the auto action so that overriding the behavior is  
> natural and any extended logic can be handled when and where desired.
>> <http://blog.afoolishmanifesto.com>
>> ------------------------------------------------------------------------




More information about the Catalyst mailing list