Ticket #7970: partials_docs.diff
| File partials_docs.diff, 6.4 kB (added by jeremymcanally, 1 year ago) |
|---|
-
actionpack/lib/action_view/partials.rb
old new 1 1 module ActionView 2 # There's also a convenience method for rendering sub templates within the current controller that depends on a single object3 # (we call this kind of sub templates for partials). It relies on the fact that partials should follow the naming convention of being4 # prefixed with an underscore -- as to separate them from regular templates that could be rendered on their own.2 # Partials are subtemplates that are rendered within the current controller that depend on a single object that is passed in as an 3 # instance variable. Partials differ from normal subtemplates in that they don't have access to the parent template's environment or 4 # variables (but you can pass variables in). 5 5 # 6 # In a template for Advertiser#account: 6 # == Partials basics 7 # Partials are typically used when you're rendering part of a template that relies on a single instance variable, which is passed in as a 8 # local variable. This variable is named for the partial's name, so if the partial is named <tt>book_info</tt>, then the instance 9 # variable will be called <tt>@book_info</tt> in the parent template and the local <tt>book_info</tt> in the partial. This means that 10 # in your view, you need to set <tt>@book_info</tt> to be whatever value you want to pass to the partial. 7 11 # 12 # To separate them from regular templates that could be rendered on their own, partials should follow the naming convention of being 13 # prefixed with an underscore (_). So, for example, you would have "_login.erb" rather than "login.erb". This naming doesn't affect 14 # how they're called in code though, so when rendering a partial, only the name is required, not the underscore. 15 # 16 # For example, in a template for Advertiser#view_info (advertiser/view_info.erb), you may want to render the advertiser's account 17 # information through a partial. To do so, you could do something like the following. 18 # 8 19 # <%= render :partial => "account" %> 9 20 # 10 21 # This would render "advertiser/_account.erb" and pass the instance variable @account in as a local variable +account+ to 11 # the template for display. 22 # the template for display. In the partial, then, you could use the account variable as a local. If it were a hash of values, 23 # it might look something like the following. 12 24 # 13 # In another template for Advertiser#buy, we could have: 25 # Account name: <%= account[:name] %> 26 # Account number: #<%= account[:number] %> 27 # Owner: <%= account[:owner] %> 14 28 # 29 # === Passing in other local variables 30 # In addition to the partial-named instance variable, you can also provide a hash of values to be used as local variables in the partial. 31 # In another part of our advertisement application, in a template for Advertiser#buy, we could have: 32 # 15 33 # <%= render :partial => "account", :locals => { :account => @buyer } %> 16 34 # 17 35 # <% for ad in @advertisements %> … … 19 37 # <% end %> 20 38 # 21 39 # This would first render "advertiser/_account.erb" with @buyer passed in as the local variable +account+, then render 22 # "advertiser/_ad.erb" and pass the local variable +ad+ to the template for display. 40 # "advertiser/_ad.erb" and pass the local variable +ad+ to the template for display. You can also pass more than one 41 # variable in as a local, since you are passing a hash in. 23 42 # 43 # <%= render :partial => "account_info", :locals => { :project => get_project(3), :user => current_user } 44 # 45 # The above code would pass in <tt>project</tt>, <tt>user</tt>, and the instance variable <tt>account_info</tt> as locals. 46 # 47 # === Sharing partials 48 # Like other templates and subtemplates, two or more controllers can share a set of partials. Rendering them is similar to other 49 # template types, and looks something like this: 50 # 51 # <%= render :partial => "advertisement/ad", :locals => { :ad => @advertisement } %> 52 # 53 # This will render the partial "advertisement/_ad.erb" regardless of which controller this is being called from. 54 # 24 55 # == Rendering a collection of partials 25 56 # 26 # The example of partial use describes a familiar pattern wherea template needs to iterate over an array and render a sub27 # template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders28 # a partial by the same name as the elements contained within. So the three-lined example in "Using partials" can be rewritten29 # with a single line:57 # A familiar pattern for using partials is when a template needs to iterate over an array and render a sub 58 # template for each of the elements. As a response, this pattern has been implemented as a single method that accepts an array and renders 59 # a partial by the same name as the elements contained within. So, rather than using a for loop or some similar construction to iterate 60 # over elements, rending a partial for each one, you can use the <tt>collection</tt> parameter for the <tt>render</tt> method. 30 61 # 31 62 # <%= render :partial => "ad", :collection => @advertisements %> 32 63 # 33 64 # This will render "advertiser/_ad.erb" and pass the local variable +ad+ to the template for display. An iteration counter 34 # will automatically be made available to the template with a name of the form +partial_name_counter+ . In the case of the35 # example above, the template would be fed +ad_counter+.65 # will automatically be made available to the template with a name of the form +partial_name_counter+ (for example, the above example 66 # template would be fed +ad_counter+), you can use that to indicate counts and so on. 36 67 # 37 # NOTE: Due to backwards compatibility concerns, the collection can't be one of hashes. Normally you'd also just keep domain objects, 38 # like Active Records, in there. 68 # NOTE: Due to backwards compatibility concerns, the collection can't be a collection of hashes. 39 69 # 40 # == Rendering shared partials41 #42 # Two controllers can share a set of partials and render them like this:43 #44 # <%= render :partial => "advertisement/ad", :locals => { :ad => @advertisement } %>45 #46 # This will render the partial "advertisement/_ad.erb" regardless of which controller this is being called from.47 70 module Partials 48 71 private 49 72 # Deprecated, use render :partial