[Catalyst] Picking template type based on input

Tomas Doran bobtfish at bobtfish.net
Fri Mar 26 12:50:45 GMT 2010


Jon mailinglists wrote:
> Hi all,
> I'm making a small catalyst application and I want to be able to serve
> different types of content based on parameters and/or request headers.
> I'm curious about where the "correct" location for that kind of code
> is, my current sollution is in the Root end like this:
> 
> sub end : ActionClass('RenderView') {
> ...
> if ($c->stash->{json} || $c->request->params->{json} ||
> $c->req->header('accept') =~ /json/)
> {
>    $c->stash->{template} = $c->action.'_json.tt';
>    $c->res->headers->content_type( 'Application/json; charset=utf-8' );
> }
> ...
> }

How is generating JSON in template toolkit not 100% insane?

Please use something like Catalyst::View::JSON instead?

> but it feels "wrong" to put it in the Root controller rather than the
> view. Is this really the place where I'm supposed to have that kind
> of logic? Would that also be the place if I want to add pdf out to
> pick another View to forward to?

Yes.

The controller asks the model for some data, and then chooses how to 
present that data (whilst the view actually handles the presentation 
details.

In the example of a PDF, you'd set $c->stash('current_view' => 'PDF');

Cheers
t0m




More information about the Catalyst mailing list