<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>OnRails.org: Short circuiting partials</title>
    <link>http://onrails.org/articles/2006/08/15/short-circuiting-partials</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Ruby On Rails and related matters.</description>
    <item>
      <title>Short circuiting partials</title>
      <description>I tend to think of partials as mini-components.  I don't like it when the page using the partial "knows" too much about what's in the partial.  For instance, if your partial displays a collection of things but shouldn't display anything if the collection is empty, then you'll usually see something like this:

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;outer page stuff...
&amp;lt;% unless some_object.has_many_things.empty? %&amp;gt;
  &amp;lt;h3&amp;gt;My collection of stuff&amp;lt;/h3&amp;gt;
  &amp;lt;%= render :partial =&amp;gt; 'my_partial', :locals =&amp;gt; { :collection_of_stuff =&amp;gt; some_object.has_many_things } %&amp;gt;
&amp;lt;% end %&amp;gt;
more outer page stuff...&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

The outer page has to know which collection of things the partial is going to use and performs some display logic around that.  Shouldn't this all be contained in the partial?  The outer page should just know that it is going to use a partial and that's it.  So our outer page will look like this:

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;outer page stuff...
&amp;lt;%= render :partial =&amp;gt; 'my_partial', :locals =&amp;gt; { :some_object =&amp;gt; some_object } %&amp;gt;
more outer page stuff...&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

Now it looks like a self-contained component.  Good.  Of course, we now have to put that outer display logic in the partial.

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;&amp;lt;% unless some_object.has_many_things.empty? %&amp;gt;
  &amp;lt;h3&amp;gt;My collection of stuff&amp;lt;/h3&amp;gt;
  &amp;lt;% some_object.has_many_things.each do |o| %&amp;gt;
    do some stuff with &amp;lt;%= o %&amp;gt;
  &amp;lt;% end %&amp;gt;
&amp;lt;% end %&amp;gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

Now the messy part is the big &lt;code&gt;unless&lt;/code&gt; block around the whole partial.  I'd like to just short circuit the whole page if there's nothing to display, similar to what I do in normal methods:

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;def do_something(some_arg)
  return nil unless some_arg
  do_something_cool_with_some_arg
end&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

Can it be as simple as putting a &lt;code&gt;return&lt;/code&gt; statement in our partial?  It turns out, yes, it is that easy.

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;&amp;lt;% return if some_object.has_many_things.empty? %&amp;gt;
&amp;lt;h3&amp;gt;My collection of stuff&amp;lt;/h3&amp;gt;
&amp;lt;% some_object.has_many_things.each do |o| %&amp;gt;
  do some stuff with &amp;lt;%= o %&amp;gt;
&amp;lt;% end %&amp;gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

Wow!  One whole line shorter.  It doesn't look like much in this small contrived example, but I think it makes the purpose of the partial clearer and doesn't clutter up the calling page.  Give it a shot.</description>
      <pubDate>Tue, 15 Aug 2006 16:26:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bb8436b2-87db-4382-bdf9-e61505efcb04</guid>
      <author>Lee Marlow</author>
      <link>http://onrails.org/articles/2006/08/15/short-circuiting-partials</link>
      <category>Rails</category>
      <category>partials</category>
      <category>components</category>
      <category>views</category>
    </item>
    <item>
      <title>"Short circuiting partials" by Lee Marlow</title>
      <description>&lt;p&gt;wow,&lt;/p&gt;


	&lt;p&gt;Cutting out one line and a level of indentation is just a pleasant side-effect.  I believe the main benefit is the partial has become more of a stand-alone component by consolidating the view logic.&lt;/p&gt;


	&lt;p&gt;-Lee&lt;/p&gt;</description>
      <pubDate>Fri, 25 Aug 2006 08:10:11 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:fbbe2be7-5dc6-426f-b558-d5db83186552</guid>
      <link>http://onrails.org/articles/2006/08/15/short-circuiting-partials#comment-134</link>
    </item>
    <item>
      <title>"Short circuiting partials" by wow</title>
      <description>&lt;p&gt;u save 1 line&amp;#8230; dude its only an end&amp;#8230; how much of a deal is that&amp;#8230;&lt;/p&gt;</description>
      <pubDate>Thu, 24 Aug 2006 17:51:26 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d0b41a2b-733a-4619-8fcf-33c19ccd0043</guid>
      <link>http://onrails.org/articles/2006/08/15/short-circuiting-partials#comment-133</link>
    </item>
    <item>
      <title>"Short circuiting partials" by Nicholas Wright</title>
      <description>&lt;p&gt;That&amp;#8217;s the first useful thing I&amp;#8217;ve ever gotten from you, Lee.&lt;/p&gt;


	&lt;p&gt;Maybe I&amp;#8217;ll stop skipping over the posts with your name on them.&lt;/p&gt;


	&lt;p&gt;Actually, having typed that, you just arrived at work, and reminded me what you&amp;#8217;re like..&lt;/p&gt;</description>
      <pubDate>Wed, 23 Aug 2006 08:56:16 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:dd4f0049-9453-4a41-980e-5d194d261813</guid>
      <link>http://onrails.org/articles/2006/08/15/short-circuiting-partials#comment-132</link>
    </item>
    <item>
      <title>"Short circuiting partials" by Lee Marlow</title>
      <description>Actually, you can return a string instead of just returning.  You could also have a block at the top of you page to check for the condition you want, output some message and then call return.  You can basically think of the partial as a method that is returning text for output.</description>
      <pubDate>Tue, 15 Aug 2006 23:09:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bf6a5813-6eae-4ef4-a9d5-f51453ac48af</guid>
      <link>http://onrails.org/articles/2006/08/15/short-circuiting-partials#comment-128</link>
    </item>
    <item>
      <title>"Short circuiting partials" by Mike</title>
      <description>Usually I use the conditional in a partial's outer page to display a message if there aren't any records to render, aka "No entries to display." While your method is obviously a cleaner approach, how would you account for letting the user know that something is wrong instead of just displaying a blank page?</description>
      <pubDate>Tue, 15 Aug 2006 19:12:21 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ed677f12-28f4-4100-b0f1-71164b8344dd</guid>
      <link>http://onrails.org/articles/2006/08/15/short-circuiting-partials#comment-127</link>
    </item>
  </channel>
</rss>
