Skip to content
This repository
Browse code

Additional detail on rendering partials

  • Loading branch information...
commit 03626f0c13c0205becf04d8a47ee1e9bf4b80cb4 1 parent 086c8c2
Mike Gunderloy authored
46  railties/doc/guides/actionview/layouts_and_rendering.txt
@@ -635,6 +635,17 @@ Here, the +_ad_banner.html.erb+ and +_footer.html.erb+ partials could contain co
635 635
 
636 636
 TIP: For content that is shared among all pages in your application, you can use partials directly from layouts.
637 637
 
  638
+==== Partial Layouts
  639
+
  640
+A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:
  641
+
  642
+[source, html]
  643
+-------------------------------------------------------
  644
+<%= render :partial => "link_area", :layout => "graybar" %>
  645
+-------------------------------------------------------
  646
+
  647
+This would look for a partial named +_link_area.html.erb+ and render it using the layout +_graybar.html.erb+. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master +layouts+ folder).
  648
+
638 649
 ==== Passing Local Variables
639 650
 
640 651
 You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content:
@@ -711,6 +722,40 @@ You can also specify a second partial to be rendered between instances of the ma
711 722
 
712 723
 Rails will render the +_product_ruler+ partial (with no data passed in to it) between each pair of +_product+ partials.
713 724
 
  725
+There's also a shorthand syntax available for rendering collections. For example, if +@products+ is a collection of products, you can render the collection this way:
  726
+
  727
+[source, html]
  728
+-------------------------------------------------------
  729
+index.rhtml.erb:
  730
+
  731
+<h1>Products</h1>
  732
+<%= render :partial => @products %>
  733
+
  734
+_product.html.erb:
  735
+
  736
+<p>Product Name: <%= product.name %></p>
  737
+-------------------------------------------------------
  738
+
  739
+Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:
  740
+
  741
+[source, html]
  742
+-------------------------------------------------------
  743
+index.rhtml.erb:
  744
+
  745
+<h1>Contacts</h1>
  746
+<%= render :partial => [customer1, employee1, customer2, employee2] %>
  747
+
  748
+_customer.html.erb:
  749
+
  750
+<p>Name: <%= customer.name %></p>
  751
+
  752
+_employee.html.erb:
  753
+
  754
+<p>Name: <%= employee.name %></p>
  755
+-------------------------------------------------------
  756
+
  757
+In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.
  758
+
714 759
 == Changelog ==
715 760
 
716 761
 http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15[Lighthouse ticket]
@@ -758,4 +803,3 @@ http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15[Lighthouse
758 803
 
759 804
 
760 805
 
761  
-

0 notes on commit 03626f0

Please sign in to comment.
Something went wrong with that request. Please try again.