2004-11-23 20:04:44 -05:00
|
|
|
module ActionView
|
2007-03-13 01:06:41 -04:00
|
|
|
# There's also a convenience method for rendering sub templates within the current controller that depends on a single object
|
|
|
|
# (we call this kind of sub templates for partials). It relies on the fact that partials should follow the naming convention of being
|
|
|
|
# prefixed with an underscore -- as to separate them from regular templates that could be rendered on their own.
|
2005-04-16 12:08:29 -04:00
|
|
|
#
|
|
|
|
# In a template for Advertiser#account:
|
|
|
|
#
|
2005-06-21 12:59:04 -04:00
|
|
|
# <%= render :partial => "account" %>
|
2005-04-16 12:08:29 -04:00
|
|
|
#
|
2007-03-13 01:06:41 -04:00
|
|
|
# This would render "advertiser/_account.erb" and pass the instance variable @account in as a local variable +account+ to
|
2005-04-16 12:08:29 -04:00
|
|
|
# the template for display.
|
|
|
|
#
|
|
|
|
# In another template for Advertiser#buy, we could have:
|
|
|
|
#
|
2005-06-21 12:59:04 -04:00
|
|
|
# <%= render :partial => "account", :locals => { :account => @buyer } %>
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
|
|
|
# <% for ad in @advertisements %>
|
2005-06-21 12:59:04 -04:00
|
|
|
# <%= render :partial => "ad", :locals => { :ad => ad } %>
|
2004-11-23 20:04:44 -05:00
|
|
|
# <% end %>
|
|
|
|
#
|
2007-03-13 01:06:41 -04:00
|
|
|
# This would first render "advertiser/_account.erb" with @buyer passed in as the local variable +account+, then render
|
2007-02-20 17:09:12 -05:00
|
|
|
# "advertiser/_ad.erb" and pass the local variable +ad+ to the template for display.
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
|
|
|
# == Rendering a collection of partials
|
|
|
|
#
|
2005-02-07 09:15:53 -05:00
|
|
|
# The example of partial use describes a familiar pattern where a template needs to iterate over an array and render a sub
|
2004-11-23 20:04:44 -05:00
|
|
|
# template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders
|
|
|
|
# a partial by the same name as the elements contained within. So the three-lined example in "Using partials" can be rewritten
|
|
|
|
# with a single line:
|
|
|
|
#
|
2005-06-21 12:59:04 -04:00
|
|
|
# <%= render :partial => "ad", :collection => @advertisements %>
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2007-02-20 17:09:12 -05:00
|
|
|
# This will render "advertiser/_ad.erb" and pass the local variable +ad+ to the template for display. An iteration counter
|
2007-03-13 01:06:41 -04:00
|
|
|
# will automatically be made available to the template with a name of the form +partial_name_counter+. In the case of the
|
2004-11-23 20:04:44 -05:00
|
|
|
# example above, the template would be fed +ad_counter+.
|
2005-07-03 04:05:05 -04:00
|
|
|
#
|
|
|
|
# NOTE: Due to backwards compatibility concerns, the collection can't be one of hashes. Normally you'd also just keep domain objects,
|
|
|
|
# like Active Records, in there.
|
2007-03-13 01:06:41 -04:00
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# == Rendering shared partials
|
|
|
|
#
|
|
|
|
# Two controllers can share a set of partials and render them like this:
|
|
|
|
#
|
2005-06-21 12:59:04 -04:00
|
|
|
# <%= render :partial => "advertisement/ad", :locals => { :ad => @advertisement } %>
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2007-02-20 17:09:12 -05:00
|
|
|
# This will render the partial "advertisement/_ad.erb" regardless of which controller this is being called from.
|
2004-11-23 20:04:44 -05:00
|
|
|
module Partials
|
2007-03-13 01:06:41 -04:00
|
|
|
private
|
|
|
|
# Deprecated, use render :partial
|
|
|
|
def render_partial(partial_path, local_assigns = nil, deprecated_local_assigns = nil) #:nodoc:
|
2007-05-17 20:36:14 -04:00
|
|
|
case partial_path
|
|
|
|
when String, Symbol, NilClass
|
|
|
|
path, partial_name = partial_pieces(partial_path)
|
|
|
|
object = extracting_object(partial_name, local_assigns, deprecated_local_assigns)
|
|
|
|
local_assigns = extract_local_assigns(local_assigns, deprecated_local_assigns)
|
|
|
|
local_assigns = local_assigns ? local_assigns.clone : {}
|
|
|
|
add_counter_to_local_assigns!(partial_name, local_assigns)
|
|
|
|
add_object_to_local_assigns!(partial_name, local_assigns, object)
|
2005-04-16 12:06:29 -04:00
|
|
|
|
2007-05-17 20:36:14 -04:00
|
|
|
if logger
|
|
|
|
ActionController::Base.benchmark("Rendered #{path}/_#{partial_name}", Logger::DEBUG, false) do
|
|
|
|
render("#{path}/_#{partial_name}", local_assigns)
|
|
|
|
end
|
|
|
|
else
|
2007-03-13 01:06:41 -04:00
|
|
|
render("#{path}/_#{partial_name}", local_assigns)
|
|
|
|
end
|
2007-05-31 23:53:39 -04:00
|
|
|
when Array, ActiveRecord::Associations::AssociationCollection
|
2007-05-17 20:36:14 -04:00
|
|
|
if partial_path.any?
|
|
|
|
path = ActionController::RecordIdentifier.partial_path(partial_path.first)
|
|
|
|
collection = partial_path
|
|
|
|
render_partial_collection(path, collection, nil, local_assigns.value)
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
2007-03-13 01:06:41 -04:00
|
|
|
else
|
2007-05-17 20:36:14 -04:00
|
|
|
render_partial(
|
|
|
|
ActionController::RecordIdentifier.partial_path(partial_path),
|
|
|
|
local_assigns, deprecated_local_assigns)
|
2005-11-16 04:00:23 -05:00
|
|
|
end
|
2005-10-01 15:00:06 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2007-03-13 01:06:41 -04:00
|
|
|
# Deprecated, use render :partial, :collection
|
|
|
|
def render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = nil) #:nodoc:
|
|
|
|
collection_of_partials = Array.new
|
|
|
|
counter_name = partial_counter_name(partial_name)
|
|
|
|
local_assigns = local_assigns ? local_assigns.clone : {}
|
|
|
|
collection.each_with_index do |element, counter|
|
|
|
|
local_assigns[counter_name] = counter
|
|
|
|
collection_of_partials.push(render_partial(partial_name, element, local_assigns))
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2007-03-13 01:06:41 -04:00
|
|
|
return " " if collection_of_partials.empty?
|
2005-05-13 12:27:36 -04:00
|
|
|
|
2007-03-13 01:06:41 -04:00
|
|
|
if partial_spacer_template
|
|
|
|
spacer_path, spacer_name = partial_pieces(partial_spacer_template)
|
|
|
|
collection_of_partials.join(render("#{spacer_path}/_#{spacer_name}"))
|
|
|
|
else
|
|
|
|
collection_of_partials.join
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-03-13 01:06:41 -04:00
|
|
|
|
|
|
|
alias_method :render_collection_of_partials, :render_partial_collection
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def partial_pieces(partial_path)
|
|
|
|
if partial_path.include?('/')
|
|
|
|
return File.dirname(partial_path), File.basename(partial_path)
|
|
|
|
else
|
2005-02-14 20:45:35 -05:00
|
|
|
return controller.class.controller_path, partial_path
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
2004-12-12 06:48:01 -05:00
|
|
|
|
|
|
|
def partial_counter_name(partial_name)
|
2005-09-27 12:45:39 -04:00
|
|
|
"#{partial_name.split('/').last}_counter".intern
|
2004-12-12 06:48:01 -05:00
|
|
|
end
|
2007-03-13 01:06:41 -04:00
|
|
|
|
2005-04-16 12:06:29 -04:00
|
|
|
def extracting_object(partial_name, local_assigns, deprecated_local_assigns)
|
|
|
|
if local_assigns.is_a?(Hash) || local_assigns.nil?
|
|
|
|
controller.instance_variable_get("@#{partial_name}")
|
|
|
|
else
|
|
|
|
# deprecated form where object could be passed in as second parameter
|
|
|
|
local_assigns
|
|
|
|
end
|
|
|
|
end
|
2007-03-13 01:06:41 -04:00
|
|
|
|
2005-04-16 12:06:29 -04:00
|
|
|
def extract_local_assigns(local_assigns, deprecated_local_assigns)
|
|
|
|
local_assigns.is_a?(Hash) ? local_assigns : deprecated_local_assigns
|
|
|
|
end
|
2007-03-13 01:06:41 -04:00
|
|
|
|
2005-04-16 12:06:29 -04:00
|
|
|
def add_counter_to_local_assigns!(partial_name, local_assigns)
|
|
|
|
counter_name = partial_counter_name(partial_name)
|
|
|
|
local_assigns[counter_name] = 1 unless local_assigns.has_key?(counter_name)
|
|
|
|
end
|
2005-11-16 04:00:23 -05:00
|
|
|
|
2005-10-10 16:35:13 -04:00
|
|
|
def add_object_to_local_assigns!(partial_name, local_assigns, object)
|
2005-11-17 14:08:53 -05:00
|
|
|
local_assigns[partial_name.intern] ||=
|
|
|
|
if object.is_a?(ActionView::Base::ObjectWrapper)
|
|
|
|
object.value
|
|
|
|
else
|
|
|
|
object
|
|
|
|
end || controller.instance_variable_get("@#{partial_name}")
|
2005-10-10 16:35:13 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|