[Catalyst] Setting View

Matt S Trout dbix-class at trout.me.uk
Sat May 20 15:10:00 CEST 2006


Jon wrote:
> Hi,
> 
> I'm doing a small project where I use (and learn) Catalyst. I've decided 
> to use some Ajax in it, MochiKit, and I want JSON templates for that. 
> Currently I'm using TT for all templates but browsing CPAN I saw there's 
> a View::JSON which seems better to use. Now, I have two questions 
> regarding this:
> 
> Is Catalyst-View-JSON-0.09 The Right Thing to use for json? First 
> release 04 Jan, and last release 10. Gives the impression of not being 
> an active and robust module that follows the Catalyst progress.

It's not had another release because it already works and hasn't AFAIK 
had any bugs reported against it.

I implemented a JSON-REST API recently using this and it worked perfectly.

> It's often mentioned how simple it is to switch between HTML/XML/FOO by 
> using different views in Catalyst, but I haven't found documentation on 
> how the prefered way of picking template/language/template system. For 
> me (coming from Apache::CGI) I'd want to put it like this:
> 
> 1 Set base name of template in Controller method, ie 
> $c->stash->{template} = 'foo/bar'
> 2 At Root::end check cookies (and/or params) for language settings and 
> append '_lang'
> 3 Still at Root:end check param 'output', 'format' or something similar 
> for what template system to use and forward to it.

The code below would work fine; you could make it even simpler by using

$c->stash->{view_type} = 'JSON'; # for example

then in your end

$c->forward($c->view($c->stash->{view_type}||'TT'));

> That is something like:
> 
> MyApp::C::Foo
> 
> sub bar : Local {
>   my ($self, $c) = @_;
>   $c->stash->{template} = 'foo/bar';
> }
> #-----------------------------------
> 
> MyApp::C::Root
> 
> sub end : Private {
>   my ($self, $c) = @_;
>   $c->stash->{template} ||= 'index';
>   if ($c->req->params->{language} && language_exists($self, $c))
>   {
>         $c->stash->{template} .= '_'.$c->req->params->{language};
>   }
>   my $view = 'MyApp::View::TT';
>   if ($c->req->params->{output} eq 'json')
>   {
>      $view = 'MyApp::View::JSON';
>   }
>   # elsif ...
>   $c->forward($view) unless $c->res->output;
> }
> 
> 
> Thanks in advance
> 
> /Jon
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Catalyst mailing list
> Catalyst at lists.rawmode.org
> http://lists.rawmode.org/mailman/listinfo/catalyst




More information about the Catalyst mailing list