[Catalyst] Multiple Views in Catalyst (FW: Catalyst-dev Digest, Vol 29, Issue 14)

Kieren Diment diment at gmail.com
Thu Nov 29 22:35:26 GMT 2007


On 30 Nov 2007, at 08:43, Emily Heureux wrote:

> Ok, I think I've gotten the correct mailing list now.   
> Unfortunately, my
> company does not allow irc, so...
>
> I would like to have more than one module in View, but I have not  
> found how
> to set that up properly.  I have spent a lot of time on cpan,  
> reading the
> tutorial, and coding examples, experimented on my project, but my  
> lack of
> experience with Catalyst makes it hard to find what I am missing.   
> It looks
> like, from the suggestions I've gotten so far, that somewhere a  
> default view
> is set (RenderView? end method?) that I need to alter.  What I  
> think would
> be a correct way of starting is,
> 1.  Use the helper script to create a second TT View.
> 2.  Somewhere, do something about the default behavior. (and what  
> if in the
> future I want more than 2 views?)
>
>

I haven't used multiple views before, so let's suck this and see.

First up:

catalyst.pl MyApp 2>&1 > /dev/null
/tmp$ cd MyApp/
/tmp/MyApp$ script/myapp_create.pl view TT TT
/tmp/MyApp$ script/myapp_create.pl view JSON JSON


let's make an index.tt:

<html>[% USE Dumper; Dumper.dump_html(data) %]</html>

alter myapp.yaml to add this:

default_view: JSON

rewrite the Controller::Root:

package MyApp::Controller::Root;

use strict;
use warnings;
use base 'Catalyst::Controller';

__PACKAGE__->config->{namespace} = '';

sub default : Private {
     my ( $self, $c ) = @_;
     $c->res->status(404);
     $c->response->body( '404 not found' );
}

sub index : Private {
     my ( $self, $c ) = @_;
     $c->stash->{current_view} = 'TT';
     $c->stash->{template}='index.tt';
}

sub json : Local {
     my ( $self, $c ) = @_;
}

sub end : ActionClass('RenderView') {
     my ($self, $c) = @_;
     $c->stash->{data} = [ $c->config->{default_view},
                           $c->stash->{current_view}
                       ];
}


1;


Next up we can fire up the server and make some requests.  Here I'm  
using the GET function from lwp-perl:

$/tmp/myapp CATALYST_DEBUG=0 script/myapp_server.pl &
$/tmp/myapp GET http://localhost:3000

<html>$VAR1 = [<br>
           'JSON',<br>
           'TT'<br>
         ];<br>
</html>


$tmp/myapp GET http://localhost:3000/json
Data":["JSON",null]}


that's about it, hth.

If your business is serious about wanting to use catalyst and wants  
to leverage community support, ask them to open up their firewall, or  
run an instance of CGI::IRC that allows access to the #catalyst,  
#dbix-class and #tt channels on irc.perl.org







More information about the Catalyst mailing list