<br><br><div class="gmail_quote">On Sat, Mar 6, 2010 at 6:34 AM, J. Shirley <span dir="ltr">&lt;<a href="mailto:jshirley@gmail.com">jshirley@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">&gt; Just one last question. When you talk about using<br>
&gt; &#39;lib/MyApp/View/MyView.pm&#39;, you mean that is also correct to create a simple<br>
&gt; perl Module for your view. Then implement process method in the View.pm and<br>
&gt; forward the context from the Controller ( $c-&gt;forward( $c-&gt;view(&#39;MyView&#39;) );<br>
&gt; )?<br>
&gt;<br>
&gt;<br>
&gt; David<br>
&gt;<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&#39;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&#39;t that seems like a View&#39;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&#39;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-&gt;forward( $c-&gt;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-&gt;action-&gt;reverse;</div>

<div><br></div><div>    $method =~ s{/}{_};</div><div><br></div><div>    $self-&gt;$method( $c ) if $self-&gt;can( $method );</div><div>}</div><div><br></div><div>But, that&#39;s flat and ugly.</div><div><br></div><div>I could keep the &quot;view&quot; 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-&gt;action-&gt;name;</div><div>$method .= &#39;_render_json&#39;;</div><div>$c-&gt;action-&gt;controller-&gt;$method( $c );</div><div><br></div><div><br></div><div>So, what&#39;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>