On Sat, Mar 6, 2010 at 6:34 AM, J. Shirley <[email protected]> wrote:
> > Just one last question. When you talk about using
> > 'lib/MyApp/View/MyView.pm', you mean that is also correct to create a
> simple
> > perl Module for your view. Then implement process method in the View.pm
> and
> > forward the context from the Controller ( $c->forward( $c->view('MyView')
> );
> > )?
> >
> >
> > David
> >
>
> Yup, that is exactly right.
>
Expanding this thread a bit...
I asked a related question in another thread. What if you want a view that
needs separate code for each action?
For example, when no template is explicitly defined in the action,
C::View::TT will pass control to a template based on the action's name.
Each action can have its own associated template.
Likewise, for a different type of output I might need code to run for each
action.
Say the controller action for the path /music/recent_uploads places a list
of objects in the stash. Rendering with HTML sends it to TT (e.g
/templates/music/recent_uploads.tt). But, what if the client wants, say an
RSS feed or JSON response for that same action? Doesn't that seems like a
View's job to turn that into a feed or JSON?
But for both of those examples I need code to serialize those objects in the
stash (into a feed or JSON). So, my question is 1) how to map the action
into a view method, and 2) where those view methods should live?
I don't think a separate view for every possible action is the correct
approach. Seems better to have in an end() method simply:
$c->forward( $c->view( $view_type ) );
A simplistic or naive View approach might look like:
sub process {
my ( $self, $c ) = @_;
my $method = $c->action->reverse;
$method =~ s{/}{_};
$self->$method( $c ) if $self->can( $method );
}
But, that's flat and ugly.
I could keep the "view" code in with the controllers which in some way makes
maintenance easier (related code is together), but then the view is not
separate.
my $method = $c->action->name;
$method .= '_render_json';
$c->action->controller->$method( $c );
So, what's the recommended approach to per-action view code?
--
Bill Moseley
[email protected]
_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/