Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / lib / action_view / partials.rb
1 module ActionView
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.
6 #
7 # In a template for Advertiser#account:
8 #
9 # <%= render :partial => "account" %>
10 #
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.
13 #
14 # In another template for Advertiser#buy, we could have:
15 #
16 # <%= render :partial => "account", :locals => { :account => @buyer } %>
17 #
18 # <% for ad in @advertisements %>
19 # <%= render :partial => "ad", :locals => { :ad => ad } %>
20 # <% end %>
21 #
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.
24 #
25 # == Rendering a collection of partials
26 #
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:
31 #
32 # <%= render :partial => "ad", :collection => @advertisements %>
33 #
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+.
37 #
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.
40 #
41 # == Rendering shared partials
42 #
43 # Two controllers can share a set of partials and render them like this:
44 #
45 # <%= render :partial => "advertisement/ad", :locals => { :ad => @advertisement } %>
46 #
47 # This will render the partial "advertisement/_ad.erb" regardless of which controller this is being called from.
48 #
49 # == Rendering objects with the RecordIdentifier
50 #
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:
53 #
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 %>
57 #
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 %>
61 #
62 # == Rendering the default case
63 #
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:
66 #
67 # # Instead of <%= render :partial => "account" %>
68 # <%= render "account" %>
69 #
70 # # Instead of <%= render :partial => "account", :locals => { :account => @buyer } %>
71 # <%= render "account", :account => @buyer %>
72 #
73 # # @account is an Account instance, so it uses the RecordIdentifier to replace
74 # # <%= render :partial => "accounts/account", :locals => { :account => @account } %>
75 # <%= render(@account) %>
76 #
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) %>
80 #
81 # == Rendering partials with layouts
82 #
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
85 # of users:
86 #
87 # <%# app/views/users/index.html.erb &>
88 # Here's the administrator:
89 # <%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %>
90 #
91 # Here's the editor:
92 # <%= render :partial => "user", :layout => "editor", :locals => { :user => editor } %>
93 #
94 # <%# app/views/users/_user.html.erb &>
95 # Name: <%= user.name %>
96 #
97 # <%# app/views/users/_administrator.html.erb &>
98 # <div id="administrator">
99 # Budget: $<%= user.budget %>
100 # <%= yield %>
101 # </div>
102 #
103 # <%# app/views/users/_editor.html.erb &>
104 # <div id="editor">
105 # Deadline: <%= user.deadline %>
106 # <%= yield %>
107 # </div>
108 #
109 # ...this will return:
110 #
111 # Here's the administrator:
112 # <div id="administrator">
113 # Budget: $<%= user.budget %>
114 # Name: <%= user.name %>
115 # </div>
116 #
117 # Here's the editor:
118 # <div id="editor">
119 # Deadline: <%= user.deadline %>
120 # Name: <%= user.name %>
121 # </div>
122 #
123 # You can also apply a layout to a block within any template:
124 #
125 # <%# app/views/users/_chief.html.erb &>
126 # <% render(:layout => "administrator", :locals => { :user => chief }) do %>
127 # Title: <%= chief.title %>
128 # <% end %>
129 #
130 # ...this will return:
131 #
132 # <div id="administrator">
133 # Budget: $<%= user.budget %>
134 # Title: <%= chief.name %>
135 # </div>
136 #
137 # As you can see, the <tt>:locals</tt> hash is shared between both the partial and its layout.
138 #
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.
141 #
142 # <%# app/views/users/_user.html.erb &>
143 # <div class="user">
144 # Budget: $<%= user.budget %>
145 # <%= yield user %>
146 # </div>
147 #
148 # <%# app/views/users/index.html.erb &>
149 # <% render :layout => @users do |user| %>
150 # Title: <%= user.title %>
151 # <% end %>
152 #
153 # This will render the layout for each user and yield to the block, passing the user, each time.
154 #
155 # You can also yield multiple times in one layout and use block arguments to differentiate the sections.
156 #
157 # <%# app/views/users/_user.html.erb &>
158 # <div class="user">
159 # <%= yield user, :header %>
160 # Budget: $<%= user.budget %>
161 # <%= yield user, :footer %>
162 # </div>
163 #
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 %>
170 # <%- end -%>
171 # <% end %>
172 module Partials
173 extend ActiveSupport::Memoizable
174
175 private
176 def render_partial(options = {}) #:nodoc:
177 local_assigns = options[:locals] || {}
178
179 case partial_path = options[:partial]
180 when String, Symbol, NilClass
181 if options.has_key?(:collection)
182 render_partial_collection(options)
183 else
184 _pick_partial_template(partial_path).render_partial(self, options[:object], local_assigns)
185 end
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)
190 else
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))
196 else
197 object = partial_path
198 render_partial(
199 :partial => ActionController::RecordIdentifier.partial_path(object, controller.class.controller_path),
200 :object => object,
201 :locals => local_assigns
202 )
203 end
204 end
205 end
206
207 def render_partial_collection(options = {}) #:nodoc:
208 return nil if options[:collection].blank?
209
210 partial = options[:partial]
211 spacer = options[:spacer_template] ? render(:partial => options[:spacer_template]) : ''
212 local_assigns = options[:locals] ? options[:locals].clone : {}
213 as = options[:as]
214
215 index = 0
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)
222 index += 1
223 result
224 end.join(spacer)
225 end
226
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)}")
230 elsif controller
231 path = "#{controller.class.controller_path}/_#{partial_path}"
232 else
233 path = "_#{partial_path}"
234 end
235
236 self.view_paths.find_template(path, self.template_format)
237 end
238 memoize :_pick_partial_template
239 end
240 end