[Catalyst] Best practices: XML output from static XML
Bill Moseley
moseley at hank.org
Sat Mar 6 16:25:24 GMT 2010
On Sat, Mar 6, 2010 at 6:34 AM, J. Shirley <jshirley at gmail.com> 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 ) =3D @_;
my $method =3D $c->action->reverse;
$method =3D~ 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 =3D $c->action->name;
$method .=3D '_render_json';
$c->action->controller->$method( $c );
So, what's the recommended approach to per-action view code?
-- =
Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20100306/e3865=
1b1/attachment.htm
More information about the Catalyst
mailing list