[Catalyst] Setting View

Matt S Trout dbix-class at trout.me.uk
Sun May 21 15:19:23 CEST 2006


Jon wrote:
> On 5/20/06, *Matt S Trout* <dbix-class at trout.me.uk 
> <mailto:dbix-class at trout.me.uk>> wrote:
> 
>     Jon wrote:
>      > Hi,
>      >
> 
> 
> [snip] 
> 
>      >
>      > 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.
> 
> 
> Nice, I just got a bit worried it was yet another module someone thought 
> would be nice, started making and stoped making after getting tired of 
> it. Since it seems to be ready I'll go for it.
> 
>      > 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'));
> 
> 
> Hmm, I'm not sure I follow you there, where would you want the stash to 
> happend? I'd prefer the controllers not to know anything about the 
> template their data is used in (I'm pretty new to MVC so there might be 
> something I miss, or I might be overzealotish trying to learn how to do 
> it The Right Way).

Setting the stash is what I'd do in place of using a request parameter; 
you can do this during begin() or something but just using it as a stash 
var in end() allows you to abstract away what it came from - you might 
for example want to select based on the Accept: header the client sends 
or some other environmental question.

> After some thinking while re-reading the stash you put there I'd change 
> my original three steps to be:
> 1. Set template type in begin where I untaint all paramters 
> ($c->stash->{view_type} or something)
> 2. Set template name in controller getting called by URI
> 3. Set language in the END by appending _$language depinging on member 
> setting/cookie/param
> 4. Add .type ($template .= '.tt' if $view_type = 'TT' ) in end depending 
> of template system so I can have index.tt <http://index.tt>, index.json, 
> index_se.tt without changing anything
> 
> Is this remotely what you mean or am I on thin ice in deep waters?

Seems reasonable, although why not use normal I18N stuff for the 
language part rather than having a separate index_se template etc.?



More information about the Catalyst mailing list