<br><br><div class="gmail_quote">On Sat, Mar 6, 2010 at 6:34 AM, J. Shirley <span dir="ltr"><<a href="mailto:jshirley@gmail.com">jshirley@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">> Just one last question. When you talk about using<br>
> 'lib/MyApp/View/MyView.pm', you mean that is also correct to create a simple<br>
> perl Module for your view. Then implement process method in the View.pm and<br>
> forward the context from the Controller ( $c->forward( $c->view('MyView') );<br>
> )?<br>
><br>
><br>
> David<br>
><br>
<br>
</div>Yup, that is exactly right.<br></blockquote><div><br></div><div>Expanding this thread a bit... </div><div><br></div><div>I asked a related question in another thread. What if you want a view that needs separate code for each action?</div>
<div><br></div><div>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.</div><div>
<br></div><div>Likewise, for a different type of output I might need code to run for each action.</div><div><br></div><div>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/<a href="http://recent_uploads.tt">recent_uploads.tt</a>). 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?</div>
<div><br></div><div>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?</div>
<div><br></div><div>I don't think a separate view for every possible action is the correct approach. Seems better to have in an end() method simply:</div><div><br></div><div>$c->forward( $c->view( $view_type ) );</div>
<div><br></div><div><br></div><div>A simplistic or naive View approach might look like:</div><div><br></div><div>sub process {</div><div> my ( $self, $c ) = @_;</div><div><br></div><div> my $method = $c->action->reverse;</div>
<div><br></div><div> $method =~ s{/}{_};</div><div><br></div><div> $self->$method( $c ) if $self->can( $method );</div><div>}</div><div><br></div><div>But, that's flat and ugly.</div><div><br></div><div>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.</div>
<div><br></div><div>my $method = $c->action->name;</div><div>$method .= '_render_json';</div><div>$c->action->controller->$method( $c );</div><div><br></div><div><br></div><div>So, what's the recommended approach to per-action view code?</div>
<div><br></div><div><br></div><div><br></div></div>-- <br>Bill Moseley<br><a href="mailto:moseley@hank.org">moseley@hank.org</a><br>