Froze rails gems
[depot.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 partials with layouts
50 #
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
53 # of users:
54 #
55 # <%# app/views/users/index.html.erb &>
56 # Here's the administrator:
57 # <%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %>
58 #
59 # Here's the editor:
60 # <%= render :partial => "user", :layout => "editor", :locals => { :user => editor } %>
61 #
62 # <%# app/views/users/_user.html.erb &>
63 # Name: <%= user.name %>
64 #
65 # <%# app/views/users/_administrator.html.erb &>
66 # <div id="administrator">
67 # Budget: $<%= user.budget %>
68 # <%= yield %>
69 # </div>
70 #
71 # <%# app/views/users/_editor.html.erb &>
72 # <div id="editor">
73 # Deadline: <%= user.deadline %>
74 # <%= yield %>
75 # </div>
76 #
77 # ...this will return:
78 #
79 # Here's the administrator:
80 # <div id="administrator">
81 # Budget: $<%= user.budget %>
82 # Name: <%= user.name %>
83 # </div>
84 #
85 # Here's the editor:
86 # <div id="editor">
87 # Deadline: <%= user.deadline %>
88 # Name: <%= user.name %>
89 # </div>
90 #
91 # You can also apply a layout to a block within any template:
92 #
93 # <%# app/views/users/_chief.html.erb &>
94 # <% render(:layout => "administrator", :locals => { :user => chief }) do %>
95 # Title: <%= chief.title %>
96 # <% end %>
97 #
98 # ...this will return:
99 #
100 # <div id="administrator">
101 # Budget: $<%= user.budget %>
102 # Title: <%= chief.name %>
103 # </div>
104 #
105 # As you can see, the <tt>:locals</tt> hash is shared between both the partial and its layout.
106 #
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.
109 #
110 # <%# app/views/users/_user.html.erb &>
111 # <div class="user">
112 # Budget: $<%= user.budget %>
113 # <%= yield user %>
114 # </div>
115 #
116 # <%# app/views/users/index.html.erb &>
117 # <% render :layout => @users do |user| %>
118 # Title: <%= user.title %>
119 # <% end %>
120 #
121 # This will render the layout for each user and yield to the block, passing the user, each time.
122 #
123 # You can also yield multiple times in one layout and use block arguments to differentiate the sections.
124 #
125 # <%# app/views/users/_user.html.erb &>
126 # <div class="user">
127 # <%= yield user, :header %>
128 # Budget: $<%= user.budget %>
129 # <%= yield user, :footer %>
130 # </div>
131 #
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 %>
138 # <%- end -%>
139 # <% end %>
140 module Partials
141 extend ActiveSupport::Memoizable
142
143 private
144 def render_partial(options = {}) #:nodoc:
145 local_assigns = options[:locals] || {}
146
147 case partial_path = options[:partial]
148 when String, Symbol, NilClass
149 if options.has_key?(:collection)
150 render_partial_collection(options)
151 else
152 _pick_partial_template(partial_path).render_partial(self, options[:object], local_assigns)
153 end
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))
160 else
161 object = partial_path
162 render_partial(
163 :partial => ActionController::RecordIdentifier.partial_path(object, controller.class.controller_path),
164 :object => object,
165 :locals => local_assigns
166 )
167 end
168 end
169
170 def render_partial_collection(options = {}) #:nodoc:
171 return nil if options[:collection].blank?
172
173 partial = options[:partial]
174 spacer = options[:spacer_template] ? render(:partial => options[:spacer_template]) : ''
175 local_assigns = options[:locals] ? options[:locals].clone : {}
176 as = options[:as]
177
178 index = 0
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)
185 index += 1
186 result
187 end.join(spacer)
188 end
189
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)}")
193 elsif controller
194 path = "#{controller.class.controller_path}/_#{partial_path}"
195 else
196 path = "_#{partial_path}"
197 end
198
199 _pick_template(path)
200 end
201 memoize :_pick_partial_template
202 end
203 end