88410999006b7b26464411e53550455cf097a223
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 partials with layouts
51 # Partials can have their own layouts applied to them. These layouts are different than the ones that are
52 # specified globally for the entire action, but they work in a similar fashion. Imagine a list with two types
55 # <%# app/views/users/index.html.erb &>
56 # Here's the administrator:
57 # <%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %>
60 # <%= render :partial => "user", :layout => "editor", :locals => { :user => editor } %>
62 # <%# app/views/users/_user.html.erb &>
63 # Name: <%= user.name %>
65 # <%# app/views/users/_administrator.html.erb &>
66 # <div id="administrator">
67 # Budget: $<%= user.budget %>
71 # <%# app/views/users/_editor.html.erb &>
73 # Deadline: <%= user.deadline %>
77 # ...this will return:
79 # Here's the administrator:
80 # <div id="administrator">
81 # Budget: $<%= user.budget %>
82 # Name: <%= user.name %>
87 # Deadline: <%= user.deadline %>
88 # Name: <%= user.name %>
91 # You can also apply a layout to a block within any template:
93 # <%# app/views/users/_chief.html.erb &>
94 # <% render(:layout => "administrator", :locals => { :user => chief }) do %>
95 # Title: <%= chief.title %>
98 # ...this will return:
100 # <div id="administrator">
101 # Budget: $<%= user.budget %>
102 # Title: <%= chief.name %>
105 # As you can see, the <tt>:locals</tt> hash is shared between both the partial and its layout.
107 # If you pass arguments to "yield" then this will be passed to the block. One way to use this is to pass
108 # an array to layout and treat it as an enumerable.
110 # <%# app/views/users/_user.html.erb &>
112 # Budget: $<%= user.budget %>
116 # <%# app/views/users/index.html.erb &>
117 # <% render :layout => @users do |user| %>
118 # Title: <%= user.title %>
121 # This will render the layout for each user and yield to the block, passing the user, each time.
123 # You can also yield multiple times in one layout and use block arguments to differentiate the sections.
125 # <%# app/views/users/_user.html.erb &>
127 # <%= yield user, :header %>
128 # Budget: $<%= user.budget %>
129 # <%= yield user, :footer %>
132 # <%# app/views/users/index.html.erb &>
133 # <% render :layout => @users do |user, section| %>
134 # <%- case section when :header -%>
135 # Title: <%= user.title %>
136 # <%- when :footer -%>
137 # Deadline: <%= user.deadline %>
141 extend ActiveSupport
::Memoizable
144 def render_partial(options
= {}) #:nodoc:
145 local_assigns
= options
[:locals] || {}
147 case partial_path
= options
[:partial]
148 when String
, Symbol
, NilClass
149 if options
.has_key
?(:collection)
150 render_partial_collection(options
)
152 _pick_partial_template(partial_path
).render_partial(self, options
[:object], local_assigns
)
154 when ActionView
::Helpers::FormBuilder
155 builder_partial_path
= partial_path
.class.to_s
.demodulize
.underscore
.sub(/_builder$/, '')
156 local_assigns
.merge
!(builder_partial_path
.to_sym
=> partial_path
)
157 render_partial(:partial => builder_partial_path
, :object => options
[:object], :locals => local_assigns
)
158 when Array
, ActiveRecord
::Associations::AssociationCollection, ActiveRecord
::NamedScope::Scope
159 render_partial_collection(options
.except(:partial).merge(:collection => partial_path
))
161 object
= partial_path
163 :partial => ActionController
::RecordIdentifier.partial_path(object
, controller
.class.controller_path
),
165 :locals => local_assigns
170 def render_partial_collection(options
= {}) #:nodoc:
171 return nil if options
[:collection].blank
?
173 partial
= options
[:partial]
174 spacer
= options
[:spacer_template] ? render(:partial => options
[:spacer_template]) : ''
175 local_assigns
= options
[:locals] ? options
[:locals].clone
: {}
179 options
[:collection].map
do |object
|
180 _partial_path
||= partial
||
181 ActionController
::RecordIdentifier.partial_path(object
, controller
.class.controller_path
)
182 template
= _pick_partial_template(_partial_path
)
183 local_assigns
[template
.counter_name
] = index
184 result
= template
.render_partial(self, object
, local_assigns
.dup
, as
)
190 def _pick_partial_template(partial_path
) #:nodoc:
191 if partial_path
.include?('/')
192 path
= File
.join(File
.dirname(partial_path
), "_#{File.basename(partial_path)}")
194 path
= "#{controller.class.controller_path}/_#{partial_path}"
196 path
= "_#{partial_path}"
201 memoize
:_pick_partial_template