2004-11-23 20:04:44 -05:00
|
|
|
module ActionView
|
2008-08-21 22:02:10 -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
|
|
|
#
|
2008-08-21 22:02:10 -04:00
|
|
|
# This would render "advertiser/_account.erb" and pass the instance variable @account in as a local variable
|
|
|
|
# +account+ to the template for display.
|
2005-04-16 12:08:29 -04:00
|
|
|
#
|
|
|
|
# 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 %>
|
|
|
|
#
|
2008-08-21 22:02:10 -04:00
|
|
|
# This would first render "advertiser/_account.erb" with @buyer passed in as the local variable +account+, then
|
|
|
|
# render "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
|
|
|
|
#
|
2008-08-21 22:02:10 -04:00
|
|
|
# The example of partial use describes a familiar pattern where a template needs to iterate over an array and
|
|
|
|
# render a sub 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:
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2005-06-21 12:59:04 -04:00
|
|
|
# <%= render :partial => "ad", :collection => @advertisements %>
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2008-08-21 22:02:10 -04:00
|
|
|
# This will render "advertiser/_ad.erb" and pass the local variable +ad+ to the template for display. An
|
|
|
|
# iteration counter will automatically be made available to the template with a name of the form
|
|
|
|
# +partial_name_counter+. In the case of the example above, the template would be fed +ad_counter+.
|
2005-07-03 04:05:05 -04:00
|
|
|
#
|
2008-08-21 22:02:10 -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.
|
2007-08-02 16:10:03 -04:00
|
|
|
#
|
2008-11-19 08:00:16 -05:00
|
|
|
# == Rendering objects with the RecordIdentifier
|
|
|
|
#
|
|
|
|
# Instead of explicitly naming the location of a partial, you can also let the RecordIdentifier do the work if
|
|
|
|
# you're following its conventions for RecordIdentifier#partial_path. Examples:
|
|
|
|
#
|
|
|
|
# # @account is an Account instance, so it uses the RecordIdentifier to replace
|
|
|
|
# # <%= render :partial => "accounts/account", :locals => { :account => @buyer } %>
|
|
|
|
# <%= render :partial => @account %>
|
|
|
|
#
|
|
|
|
# # @posts is an array of Post instances, so it uses the RecordIdentifier to replace
|
|
|
|
# # <%= render :partial => "posts/post", :collection => @posts %>
|
|
|
|
# <%= render :partial => @posts %>
|
|
|
|
#
|
|
|
|
# == Rendering the default case
|
|
|
|
#
|
|
|
|
# If you're not going to be using any of the options like collections or layouts, you can also use the short-hand
|
|
|
|
# defaults of render to render partials. Examples:
|
|
|
|
#
|
|
|
|
# # Instead of <%= render :partial => "account" %>
|
|
|
|
# <%= render "account" %>
|
|
|
|
#
|
|
|
|
# # Instead of <%= render :partial => "account", :locals => { :account => @buyer } %>
|
|
|
|
# <%= render "account", :account => @buyer %>
|
|
|
|
#
|
|
|
|
# # @account is an Account instance, so it uses the RecordIdentifier to replace
|
|
|
|
# # <%= render :partial => "accounts/account", :locals => { :account => @account } %>
|
|
|
|
# <%= render(@account) %>
|
|
|
|
#
|
|
|
|
# # @posts is an array of Post instances, so it uses the RecordIdentifier to replace
|
|
|
|
# # <%= render :partial => "posts/post", :collection => @posts %>
|
|
|
|
# <%= render(@posts) %>
|
|
|
|
#
|
2007-08-02 16:10:03 -04:00
|
|
|
# == Rendering partials with layouts
|
|
|
|
#
|
2008-08-21 22:02:10 -04:00
|
|
|
# Partials can have their own layouts applied to them. These layouts are different than the ones that are
|
|
|
|
# specified globally for the entire action, but they work in a similar fashion. Imagine a list with two types
|
|
|
|
# of users:
|
2007-08-02 16:10:03 -04:00
|
|
|
#
|
2007-09-24 19:01:50 -04:00
|
|
|
# <%# app/views/users/index.html.erb &>
|
2007-08-02 16:10:03 -04:00
|
|
|
# Here's the administrator:
|
|
|
|
# <%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %>
|
|
|
|
#
|
|
|
|
# Here's the editor:
|
|
|
|
# <%= render :partial => "user", :layout => "editor", :locals => { :user => editor } %>
|
|
|
|
#
|
2007-09-24 19:01:50 -04:00
|
|
|
# <%# app/views/users/_user.html.erb &>
|
2007-08-02 16:10:03 -04:00
|
|
|
# Name: <%= user.name %>
|
|
|
|
#
|
2007-09-24 19:01:50 -04:00
|
|
|
# <%# app/views/users/_administrator.html.erb &>
|
2007-08-02 16:10:03 -04:00
|
|
|
# <div id="administrator">
|
|
|
|
# Budget: $<%= user.budget %>
|
|
|
|
# <%= yield %>
|
|
|
|
# </div>
|
|
|
|
#
|
2007-09-24 19:01:50 -04:00
|
|
|
# <%# app/views/users/_editor.html.erb &>
|
2007-08-02 16:10:03 -04:00
|
|
|
# <div id="editor">
|
2008-08-17 20:29:24 -04:00
|
|
|
# Deadline: <%= user.deadline %>
|
2007-08-02 16:10:03 -04:00
|
|
|
# <%= yield %>
|
|
|
|
# </div>
|
|
|
|
#
|
|
|
|
# ...this will return:
|
|
|
|
#
|
|
|
|
# Here's the administrator:
|
|
|
|
# <div id="administrator">
|
|
|
|
# Budget: $<%= user.budget %>
|
|
|
|
# Name: <%= user.name %>
|
|
|
|
# </div>
|
|
|
|
#
|
|
|
|
# Here's the editor:
|
|
|
|
# <div id="editor">
|
2008-08-17 20:29:24 -04:00
|
|
|
# Deadline: <%= user.deadline %>
|
2007-08-02 16:10:03 -04:00
|
|
|
# Name: <%= user.name %>
|
|
|
|
# </div>
|
|
|
|
#
|
|
|
|
# You can also apply a layout to a block within any template:
|
|
|
|
#
|
2007-09-24 19:01:50 -04:00
|
|
|
# <%# app/views/users/_chief.html.erb &>
|
2007-08-02 16:10:03 -04:00
|
|
|
# <% render(:layout => "administrator", :locals => { :user => chief }) do %>
|
|
|
|
# Title: <%= chief.title %>
|
|
|
|
# <% end %>
|
|
|
|
#
|
|
|
|
# ...this will return:
|
|
|
|
#
|
|
|
|
# <div id="administrator">
|
|
|
|
# Budget: $<%= user.budget %>
|
|
|
|
# Title: <%= chief.name %>
|
|
|
|
# </div>
|
|
|
|
#
|
2008-05-02 09:45:23 -04:00
|
|
|
# As you can see, the <tt>:locals</tt> hash is shared between both the partial and its layout.
|
2008-08-17 20:29:24 -04:00
|
|
|
#
|
|
|
|
# If you pass arguments to "yield" then this will be passed to the block. One way to use this is to pass
|
|
|
|
# an array to layout and treat it as an enumerable.
|
|
|
|
#
|
|
|
|
# <%# app/views/users/_user.html.erb &>
|
|
|
|
# <div class="user">
|
|
|
|
# Budget: $<%= user.budget %>
|
|
|
|
# <%= yield user %>
|
|
|
|
# </div>
|
|
|
|
#
|
|
|
|
# <%# app/views/users/index.html.erb &>
|
|
|
|
# <% render :layout => @users do |user| %>
|
|
|
|
# Title: <%= user.title %>
|
|
|
|
# <% end %>
|
|
|
|
#
|
|
|
|
# This will render the layout for each user and yield to the block, passing the user, each time.
|
|
|
|
#
|
|
|
|
# You can also yield multiple times in one layout and use block arguments to differentiate the sections.
|
|
|
|
#
|
|
|
|
# <%# app/views/users/_user.html.erb &>
|
|
|
|
# <div class="user">
|
|
|
|
# <%= yield user, :header %>
|
|
|
|
# Budget: $<%= user.budget %>
|
|
|
|
# <%= yield user, :footer %>
|
|
|
|
# </div>
|
|
|
|
#
|
|
|
|
# <%# app/views/users/index.html.erb &>
|
|
|
|
# <% render :layout => @users do |user, section| %>
|
|
|
|
# <%- case section when :header -%>
|
|
|
|
# Title: <%= user.title %>
|
|
|
|
# <%- when :footer -%>
|
|
|
|
# Deadline: <%= user.deadline %>
|
|
|
|
# <%- end -%>
|
|
|
|
# <% end %>
|
2004-11-23 20:04:44 -05:00
|
|
|
module Partials
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-08-06 22:42:11 -04:00
|
|
|
|
2009-08-08 11:26:58 -04:00
|
|
|
class PartialRenderer
|
2009-08-14 15:00:52 -04:00
|
|
|
PARTIAL_NAMES = Hash.new {|h,k| h[k] = {} }
|
|
|
|
TEMPLATES = Hash.new {|h,k| h[k] = {} }
|
2009-08-06 23:52:13 -04:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
attr_reader :template
|
2009-08-08 22:43:45 -04:00
|
|
|
|
2009-08-08 17:12:47 -04:00
|
|
|
def initialize(view_context, options, block)
|
|
|
|
@view = view_context
|
2009-08-14 15:00:52 -04:00
|
|
|
@partial_names = PARTIAL_NAMES[@view.controller.class]
|
2009-12-01 23:58:56 -05:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
key = Thread.current[:format_locale_key]
|
|
|
|
@templates = TEMPLATES[key] if key
|
2009-12-01 23:58:56 -05:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
setup(options, block)
|
|
|
|
end
|
2009-12-01 23:58:56 -05:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
def setup(options, block)
|
|
|
|
partial = options[:partial]
|
2009-12-01 23:58:56 -05:00
|
|
|
|
2009-12-26 06:43:48 -05:00
|
|
|
@options = options
|
|
|
|
@locals = options[:locals] || {}
|
|
|
|
@block = block
|
2009-12-01 23:58:56 -05:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
if String === partial
|
2009-12-26 06:43:48 -05:00
|
|
|
@object = options[:object]
|
|
|
|
@path = partial
|
|
|
|
@collection = collection
|
2009-08-14 15:00:52 -04:00
|
|
|
else
|
|
|
|
@object = partial
|
2009-12-26 06:43:48 -05:00
|
|
|
|
|
|
|
if @collection = collection
|
|
|
|
paths = @collection_paths = @collection.map { |o| partial_path(o) }
|
|
|
|
@path = paths.uniq.size == 1 ? paths.first : nil
|
|
|
|
else
|
|
|
|
@path = partial_path
|
|
|
|
end
|
2009-08-14 15:00:52 -04:00
|
|
|
end
|
2009-08-06 23:52:13 -04:00
|
|
|
end
|
|
|
|
|
2009-08-08 17:12:47 -04:00
|
|
|
def render
|
2010-01-11 04:21:15 -05:00
|
|
|
identifier = ((@template = find_template) ? @template.identifier : @path)
|
2009-12-25 17:37:16 -05:00
|
|
|
|
2009-12-26 06:43:48 -05:00
|
|
|
if @collection
|
2010-01-03 18:03:56 -05:00
|
|
|
ActiveSupport::Notifications.instrument("action_view.render_collection",
|
2010-01-11 04:21:15 -05:00
|
|
|
:identifier => identifier, :count => @collection.size) do
|
2009-12-25 17:37:16 -05:00
|
|
|
render_collection
|
|
|
|
end
|
2009-08-14 15:00:52 -04:00
|
|
|
else
|
2010-01-03 18:03:56 -05:00
|
|
|
content = ActiveSupport::Notifications.instrument("action_view.render_partial",
|
2010-01-11 04:21:15 -05:00
|
|
|
:identifier => identifier) do
|
2009-12-25 17:37:16 -05:00
|
|
|
render_partial
|
|
|
|
end
|
|
|
|
|
2010-01-11 04:21:15 -05:00
|
|
|
if !@block && (layout = @options[:layout])
|
|
|
|
content = @view._render_layout(find_template(layout), @locals){ content }
|
2009-12-25 17:37:16 -05:00
|
|
|
end
|
|
|
|
content
|
2009-08-14 15:00:52 -04:00
|
|
|
end
|
2005-10-01 15:00:06 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2009-08-08 17:12:47 -04:00
|
|
|
def render_collection
|
2009-08-14 15:00:52 -04:00
|
|
|
return nil if @collection.blank?
|
2009-08-06 22:42:11 -04:00
|
|
|
|
2009-08-08 11:26:58 -04:00
|
|
|
if @options.key?(:spacer_template)
|
2009-08-08 17:12:47 -04:00
|
|
|
spacer = find_template(@options[:spacer_template]).render(@view, @locals)
|
2009-08-07 05:32:17 -04:00
|
|
|
end
|
2009-08-07 04:40:01 -04:00
|
|
|
|
2010-01-11 04:21:15 -05:00
|
|
|
result = @template ? collection_with_template : collection_without_template
|
2009-10-07 16:31:20 -04:00
|
|
|
result.join(spacer).html_safe!
|
2009-08-10 02:54:17 -04:00
|
|
|
end
|
|
|
|
|
2009-12-26 06:43:48 -05:00
|
|
|
def collection_with_template(template = @template)
|
2009-12-25 17:37:16 -05:00
|
|
|
segments, locals, as = [], @locals, @options[:as] || template.variable_name
|
2008-03-31 20:50:09 -04:00
|
|
|
|
2009-08-10 08:57:44 -04:00
|
|
|
counter_name = template.counter_name
|
|
|
|
locals[counter_name] = -1
|
2009-08-10 02:54:17 -04:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
@collection.each do |object|
|
2009-08-10 08:57:44 -04:00
|
|
|
locals[counter_name] += 1
|
2009-08-12 02:43:15 -04:00
|
|
|
locals[as] = object
|
2009-08-10 02:54:17 -04:00
|
|
|
|
2009-08-10 08:57:44 -04:00
|
|
|
segments << template.render(@view, locals)
|
2009-08-08 16:30:04 -04:00
|
|
|
end
|
2009-12-01 23:58:56 -05:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
@template = template
|
2009-08-10 08:57:44 -04:00
|
|
|
segments
|
2009-08-10 02:54:17 -04:00
|
|
|
end
|
2009-08-06 22:42:11 -04:00
|
|
|
|
2009-12-26 06:43:48 -05:00
|
|
|
def collection_without_template(collection_paths = @collection_paths)
|
2009-12-25 17:37:16 -05:00
|
|
|
segments, locals, as = [], @locals, @options[:as]
|
2009-08-10 02:54:17 -04:00
|
|
|
index, template = -1, nil
|
|
|
|
|
2009-12-26 05:02:41 -05:00
|
|
|
@collection.each_with_index do |object, i|
|
|
|
|
template = find_template(collection_paths[i])
|
2009-08-10 08:57:44 -04:00
|
|
|
locals[template.counter_name] = (index += 1)
|
2009-12-26 06:43:48 -05:00
|
|
|
locals[as || template.variable_name] = object
|
2009-08-10 02:54:17 -04:00
|
|
|
|
2009-08-10 08:57:44 -04:00
|
|
|
segments << template.render(@view, locals)
|
2009-08-10 02:54:17 -04:00
|
|
|
end
|
2009-08-10 08:57:44 -04:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
@template = template
|
2009-08-10 08:57:44 -04:00
|
|
|
segments
|
2008-03-31 20:50:09 -04:00
|
|
|
end
|
|
|
|
|
2009-12-25 17:37:16 -05:00
|
|
|
def render_partial(object = @object)
|
|
|
|
locals, view = @locals, @view
|
2009-08-08 17:12:47 -04:00
|
|
|
|
2009-12-25 17:37:16 -05:00
|
|
|
object ||= locals[template.variable_name]
|
|
|
|
locals[@options[:as] || template.variable_name] = object
|
2009-08-08 17:12:47 -04:00
|
|
|
|
2009-12-25 17:37:16 -05:00
|
|
|
template.render(view, locals) do |*name|
|
|
|
|
view._layout_for(*name, &@block)
|
2009-08-14 15:00:52 -04:00
|
|
|
end
|
|
|
|
end
|
2009-08-08 17:12:47 -04:00
|
|
|
|
2009-08-08 11:26:58 -04:00
|
|
|
private
|
|
|
|
def collection
|
2009-08-14 15:00:52 -04:00
|
|
|
if @object.respond_to?(:to_ary)
|
2009-08-08 11:26:58 -04:00
|
|
|
@object
|
|
|
|
elsif @options.key?(:collection)
|
|
|
|
@options[:collection] || []
|
|
|
|
end
|
2005-10-10 16:35:13 -04:00
|
|
|
end
|
2009-08-08 11:26:58 -04:00
|
|
|
|
2009-08-08 16:30:04 -04:00
|
|
|
def find_template(path = @path)
|
2009-08-14 15:00:52 -04:00
|
|
|
unless @templates
|
|
|
|
path && _find_template(path)
|
|
|
|
else
|
|
|
|
path && @templates[path] ||= _find_template(path)
|
2009-08-08 16:30:04 -04:00
|
|
|
end
|
2009-08-08 11:26:58 -04:00
|
|
|
end
|
2009-12-01 23:58:56 -05:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
def _find_template(path)
|
2009-10-09 06:55:00 -04:00
|
|
|
if controller = @view.controller
|
|
|
|
prefix = controller.controller_path unless path.include?(?/)
|
|
|
|
end
|
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
@view.find(path, {:formats => @view.formats}, prefix, true)
|
|
|
|
end
|
2009-08-08 11:26:58 -04:00
|
|
|
|
|
|
|
def partial_path(object = @object)
|
2009-08-08 16:30:04 -04:00
|
|
|
@partial_names[object.class] ||= begin
|
2009-12-26 06:43:48 -05:00
|
|
|
object = object.to_model if object.respond_to?(:to_model)
|
2009-08-08 11:26:58 -04:00
|
|
|
|
2009-12-26 06:43:48 -05:00
|
|
|
object.class.model_name.partial_path.dup.tap do |partial|
|
2009-08-08 16:30:04 -04:00
|
|
|
path = @view.controller_path
|
|
|
|
partial.insert(0, "#{File.dirname(path)}/") if path.include?(?/)
|
2009-08-08 11:26:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_partial(options)
|
2009-08-11 18:02:05 -04:00
|
|
|
_evaluate_assigns_and_ivars
|
2009-08-14 15:00:52 -04:00
|
|
|
|
|
|
|
details = options[:_details]
|
2009-12-01 23:58:56 -05:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
# Is this needed
|
|
|
|
self.formats = details[:formats] if details
|
|
|
|
renderer = PartialRenderer.new(self, options, nil)
|
|
|
|
text = renderer.render
|
|
|
|
options[:_template] = renderer.template
|
|
|
|
text
|
2009-08-08 11:26:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def _render_partial(options, &block) #:nodoc:
|
2009-12-01 23:58:56 -05:00
|
|
|
if defined? @renderer
|
2009-08-14 15:00:52 -04:00
|
|
|
@renderer.setup(options, block)
|
|
|
|
else
|
|
|
|
@renderer = PartialRenderer.new(self, options, block)
|
|
|
|
end
|
2009-12-01 23:58:56 -05:00
|
|
|
|
2009-08-14 15:00:52 -04:00
|
|
|
@renderer.render
|
2009-08-08 11:26:58 -04:00
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|