2 # There's also a convenience method for rendering sub templates within the current controller that depends on a
3 # single object (we call this kind of sub templates for partials). It relies on the fact that partials should
4 # follow the naming convention of being prefixed with an underscore -- as to separate them from regular
5 # templates that could be rendered on their own.
7 # In a template for Advertiser#account:
9 # <%= render :partial => "account" %>
11 # This would render "advertiser/_account.erb" and pass the instance variable @account in as a local variable
12 # +account+ to the template for display.
14 # In another template for Advertiser#buy, we could have:
16 # <%= render :partial => "account", :locals => { :account => @buyer } %>
18 # <% for ad in @advertisements %>
19 # <%= render :partial => "ad", :locals => { :ad => ad } %>
22 # This would first render "advertiser/_account.erb" with @buyer passed in as the local variable +account+, then
23 # render "advertiser/_ad.erb" and pass the local variable +ad+ to the template for display.
25 # == Rendering a collection of partials
27 # The example of partial use describes a familiar pattern where a template needs to iterate over an array and
28 # render a sub template for each of the elements. This pattern has been implemented as a single method that
29 # accepts an array and renders a partial by the same name as the elements contained within. So the three-lined
30 # example in "Using partials" can be rewritten with a single line:
32 # <%= render :partial => "ad", :collection => @advertisements %>
34 # This will render "advertiser/_ad.erb" and pass the local variable +ad+ to the template for display. An
35 # iteration counter will automatically be made available to the template with a name of the form
36 # +partial_name_counter+. In the case of the example above, the template would be fed +ad_counter+.
38 # NOTE: Due to backwards compatibility concerns, the collection can't be one of hashes. Normally you'd also
39 # just keep domain objects, like Active Records, in there.
41 # == Rendering shared partials
43 # Two controllers can share a set of partials and render them like this:
45 # <%= render :partial => "advertisement/ad", :locals => { :ad => @advertisement } %>
47 # This will render the partial "advertisement/_ad.erb" regardless of which controller this is being called from.
49 # == Rendering objects with the RecordIdentifier
51 # Instead of explicitly naming the location of a partial, you can also let the RecordIdentifier do the work if
52 # you're following its conventions for RecordIdentifier#partial_path. Examples:
54 # # @account is an Account instance, so it uses the RecordIdentifier to replace
55 # # <%= render :partial => "accounts/account", :locals => { :account => @buyer } %>
56 # <%= render :partial => @account %>
58 # # @posts is an array of Post instances, so it uses the RecordIdentifier to replace
59 # # <%= render :partial => "posts/post", :collection => @posts %>
60 # <%= render :partial => @posts %>
62 # == Rendering the default case
64 # If you're not going to be using any of the options like collections or layouts, you can also use the short-hand
65 # defaults of render to render partials. Examples:
67 # # Instead of <%= render :partial => "account" %>
68 # <%= render "account" %>
70 # # Instead of <%= render :partial => "account", :locals => { :account => @buyer } %>
71 # <%= render "account", :account => @buyer %>
73 # # @account is an Account instance, so it uses the RecordIdentifier to replace
74 # # <%= render :partial => "accounts/account", :locals => { :account => @account } %>
75 # <%= render(@account) %>
77 # # @posts is an array of Post instances, so it uses the RecordIdentifier to replace
78 # # <%= render :partial => "posts/post", :collection => @posts %>
79 # <%= render(@posts) %>
81 # == Rendering partials with layouts
83 # Partials can have their own layouts applied to them. These layouts are different than the ones that are
84 # specified globally for the entire action, but they work in a similar fashion. Imagine a list with two types
87 # <%# app/views/users/index.html.erb &>
88 # Here's the administrator:
89 # <%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %>
92 # <%= render :partial => "user", :layout => "editor", :locals => { :user => editor } %>
94 # <%# app/views/users/_user.html.erb &>
95 # Name: <%= user.name %>
97 # <%# app/views/users/_administrator.html.erb &>
98 # <div id="administrator">
99 # Budget: $<%= user.budget %>
103 # <%# app/views/users/_editor.html.erb &>
105 # Deadline: <%= user.deadline %>
109 # ...this will return:
111 # Here's the administrator:
112 # <div id="administrator">
113 # Budget: $<%= user.budget %>
114 # Name: <%= user.name %>
119 # Deadline: <%= user.deadline %>
120 # Name: <%= user.name %>
123 # You can also apply a layout to a block within any template:
125 # <%# app/views/users/_chief.html.erb &>
126 # <% render(:layout => "administrator", :locals => { :user => chief }) do %>
127 # Title: <%= chief.title %>
130 # ...this will return:
132 # <div id="administrator">
133 # Budget: $<%= user.budget %>
134 # Title: <%= chief.name %>
137 # As you can see, the <tt>:locals</tt> hash is shared between both the partial and its layout.
139 # If you pass arguments to "yield" then this will be passed to the block. One way to use this is to pass
140 # an array to layout and treat it as an enumerable.
142 # <%# app/views/users/_user.html.erb &>
144 # Budget: $<%= user.budget %>
148 # <%# app/views/users/index.html.erb &>
149 # <% render :layout => @users do |user| %>
150 # Title: <%= user.title %>
153 # This will render the layout for each user and yield to the block, passing the user, each time.
155 # You can also yield multiple times in one layout and use block arguments to differentiate the sections.
157 # <%# app/views/users/_user.html.erb &>
159 # <%= yield user, :header %>
160 # Budget: $<%= user.budget %>
161 # <%= yield user, :footer %>
164 # <%# app/views/users/index.html.erb &>
165 # <% render :layout => @users do |user, section| %>
166 # <%- case section when :header -%>
167 # Title: <%= user.title %>
168 # <%- when :footer -%>
169 # Deadline: <%= user.deadline %>
173 extend ActiveSupport
::Memoizable
176 def render_partial(options
= {}) #:nodoc:
177 local_assigns
= options
[:locals] || {}
179 case partial_path
= options
[:partial]
180 when String
, Symbol
, NilClass
181 if options
.has_key
?(:collection)
182 render_partial_collection(options
)
184 _pick_partial_template(partial_path
).render_partial(self, options
[:object], local_assigns
)
186 when ActionView
::Helpers::FormBuilder
187 builder_partial_path
= partial_path
.class.to_s
.demodulize
.underscore
.sub(/_builder$/, '')
188 local_assigns
.merge
!(builder_partial_path
.to_sym
=> partial_path
)
189 render_partial(:partial => builder_partial_path
, :object => options
[:object], :locals => local_assigns
)
191 if Array
=== partial_path
||
192 (defined?(ActiveRecord
) &&
193 (ActiveRecord
::Associations::AssociationCollection === partial_path
||
194 ActiveRecord
::NamedScope::Scope === partial_path
))
195 render_partial_collection(options
.except(:partial).merge(:collection => partial_path
))
197 object
= partial_path
199 :partial => ActionController
::RecordIdentifier.partial_path(object
, controller
.class.controller_path
),
201 :locals => local_assigns
207 def render_partial_collection(options
= {}) #:nodoc:
208 return nil if options
[:collection].blank
?
210 partial
= options
[:partial]
211 spacer
= options
[:spacer_template] ? render(:partial => options
[:spacer_template]) : ''
212 local_assigns
= options
[:locals] ? options
[:locals].clone
: {}
216 options
[:collection].map
do |object
|
217 _partial_path
||= partial
||
218 ActionController
::RecordIdentifier.partial_path(object
, controller
.class.controller_path
)
219 template
= _pick_partial_template(_partial_path
)
220 local_assigns
[template
.counter_name
] = index
221 result
= template
.render_partial(self, object
, local_assigns
.dup
, as
)
227 def _pick_partial_template(partial_path
) #:nodoc:
228 if partial_path
.include?('/')
229 path
= File
.join(File
.dirname(partial_path
), "_#{File.basename(partial_path)}")
231 path
= "#{controller.class.controller_path}/_#{partial_path}"
233 path
= "_#{partial_path}"
236 self.view_paths
.find_template(path
, self.template_format
)
238 memoize
:_pick_partial_template