[Catalyst] Patch for Catalyst::View::Mason

Jonathan Rockway jon at jrock.us
Fri Mar 7 18:26:23 GMT 2008


* On Fri, Mar 07 2008, Matthias Zeichmann wrote:
> On Thu, Mar 6, 2008 at 11:11 PM, David Jack Wange Olrik <david at olrik.dk> wrote:
>>  Here is a small patch for Catalyst::View::Mason.
>>
>>  When setting $c->stash->{template}, template_extension was not added
>>  to the component path. In my opinion this should always be added, so
>>  one can easily change the view without having to provide extra logic
>>  that sets the template extension according to the current view.
>
> C:V:TT behaves exactly the same way [0] and i suspect its not the only
> view that does
>
>     my $template = $c->stash->{template}
>       ||  $c->action . $self->config->{TEMPLATE_EXTENSION};

This is really the only thing that makes sense.  To do what David wanted
would have to use a heuristic to determine whether $c->stash->{template}
is the actual template name, or the template name minus the extension.
By definition, that's never going to work.

Fortunately, it's easy to implement the behavior in your own app:

  package MyApp::View::Whatever;
  use base qw/Catalyst::Component::ACCEPT_CONTEXT Catalyst::View::Whatever/;
  
  sub set_template_name {
     my ($self, $name) = @_;
     my $extension = $self->{TEMPLATE_EXTENSION};
     given($self->context->request->path){
        when(/OH HAI/){
           $extension = 'something-else';
        }
     }

     return $self->context->stash->{template} = "$name.$extension";       
  }

Then you can do this:

  sub action :Path('OH_HAI') {
     my ($self, $c) = @_;
     ...
     $c->view('Whatever')->set_template_name('name-without-extension');     
  }

Then $c->stash->{template} will be 'name-without-extension.something-else'.

Regards,
Jonathan Rockway



More information about the Catalyst mailing list