aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/render
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_view/render')
-rw-r--r--actionpack/lib/action_view/render/layouts.rb26
-rw-r--r--actionpack/lib/action_view/render/partials.rb8
-rw-r--r--actionpack/lib/action_view/render/rendering.rb56
3 files changed, 35 insertions, 55 deletions
diff --git a/actionpack/lib/action_view/render/layouts.rb b/actionpack/lib/action_view/render/layouts.rb
index 8688de3d18..578f39d817 100644
--- a/actionpack/lib/action_view/render/layouts.rb
+++ b/actionpack/lib/action_view/render/layouts.rb
@@ -1,8 +1,5 @@
-require 'active_support/core_ext/object/try'
-
module ActionView
module Layouts
-
# You can think of a layout as a method that is called with a block. _layout_for
# returns the contents that are yielded to the layout. If the user calls yield
# :some_name, the block, by default, returns content_for(:some_name). If the user
@@ -13,7 +10,7 @@ module ActionView
# ==== Example
#
# # The template
- # <% render :layout => "my_layout" do %>Content<% end %>
+ # <%= render :layout => "my_layout" do %>Content<% end %>
#
# # The layout
# <html><% yield %></html>
@@ -27,7 +24,7 @@ module ActionView
# ==== Example
#
# # The template
- # <% render :layout => "my_layout" do |customer| %>Hello <%= customer.name %><% end %>
+ # <%= render :layout => "my_layout" do |customer| %>Hello <%= customer.name %><% end %>
#
# # The layout
# <html><% yield Struct.new(:name).new("David") %></html>
@@ -46,17 +43,28 @@ module ActionView
# This is the method which actually finds the layout using details in the lookup
# context object. If no layout is found, it checkes if at least a layout with
# the given name exists across all details before raising the error.
- def _find_layout(layout) #:nodoc:
+ #
+ # If self.formats contains several formats, just the first one is considered in
+ # the layout lookup.
+ def find_layout(layout)
begin
- layout =~ /^\// ?
- with_fallbacks { find(layout) } : find(layout)
+ if formats.size == 1
+ _find_layout(layout)
+ else
+ update_details(:formats => self.formats.first){ _find_layout(layout) }
+ end
rescue ActionView::MissingTemplate => e
update_details(:formats => nil) do
- raise unless exists?(layout)
+ raise unless template_exists?(layout)
end
end
end
+ def _find_layout(layout) #:nodoc:
+ layout =~ /^\// ?
+ with_fallbacks { find_template(layout) } : find_template(layout)
+ end
+
# Contains the logic that actually renders the layout.
def _render_layout(layout, locals, &block) #:nodoc:
layout.render(self, locals){ |*name| _layout_for(*name, &block) }
diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb
index 950c9d2cd8..17d16556b9 100644
--- a/actionpack/lib/action_view/render/partials.rb
+++ b/actionpack/lib/action_view/render/partials.rb
@@ -123,7 +123,7 @@ module ActionView
# You can also apply a layout to a block within any template:
#
# <%# app/views/users/_chief.html.erb &>
- # <% render(:layout => "administrator", :locals => { :user => chief }) do %>
+ # <%= render(:layout => "administrator", :locals => { :user => chief }) do %>
# Title: <%= chief.title %>
# <% end %>
#
@@ -146,7 +146,7 @@ module ActionView
# </div>
#
# <%# app/views/users/index.html.erb &>
- # <% render :layout => @users do |user| %>
+ # <%= render :layout => @users do |user| %>
# Title: <%= user.title %>
# <% end %>
#
@@ -162,7 +162,7 @@ module ActionView
# </div>
#
# <%# app/views/users/index.html.erb &>
- # <% render :layout => @users do |user, section| %>
+ # <%= render :layout => @users do |user, section| %>
# <%- case section when :header -%>
# Title: <%= user.title %>
# <%- when :footer -%>
@@ -294,7 +294,7 @@ module ActionView
def find_template(path=@path)
return path unless path.is_a?(String)
prefix = @view.controller_path unless path.include?(?/)
- @view.find(path, prefix, true)
+ @view.find_template(path, prefix, true)
end
def partial_path(object = @object)
diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb
index 47ea70f5ad..492326964a 100644
--- a/actionpack/lib/action_view/render/rendering.rb
+++ b/actionpack/lib/action_view/render/rendering.rb
@@ -12,14 +12,17 @@ module ActionView
#
# If no options hash is passed or :update specified, the default is to render a partial and use the second parameter
# as the locals hash.
- def render(options = {}, locals = {}, &block) #:nodoc:
+ def render(options = {}, locals = {}, &block)
case options
when Hash
if block_given?
- content = _render_partial(options.merge(:partial => options[:layout]), &block)
- safe_concat(content)
+ _render_partial(options.merge(:partial => options[:layout]), &block)
+ elsif options.key?(:partial)
+ _render_partial(options)
else
- _render(options)
+ template = _determine_template(options)
+ lookup_context.freeze_formats(template.formats, true)
+ _render_template(template, options[:layout], options)
end
when :update
update_page(&block)
@@ -28,44 +31,18 @@ module ActionView
end
end
- # This is the API to render a ViewContext's template from a controller.
- def render_template(options, &block)
- _evaluate_assigns_and_ivars
-
- # TODO Layout for partials should be handled here, because inside the
- # partial renderer it looks for the layout as a partial.
- if options.key?(:partial) && options[:layout]
- options[:layout] = _find_layout(options[:layout])
- end
-
- _render(options, &block)
- end
-
- # This method holds the common render logic for both controllers and
- # views rendering stacks.
- def _render(options) #:nodoc:
- if options.key?(:partial)
- _render_partial(options)
- else
- template = _determine_template(options)
- yield template if block_given?
- _render_template(template, options[:layout], options)
- end
- end
-
# Determine the template to be rendered using the given options.
def _determine_template(options) #:nodoc:
if options.key?(:inline)
handler = Template.handler_class_for_extension(options[:type] || "erb")
Template.new(options[:inline], "inline template", handler, {})
elsif options.key?(:text)
- Template::Text.new(options[:text], self.formats.try(:first))
- elsif options.key?(:_template)
- options[:_template]
+ Template::Text.new(options[:text], formats.try(:first))
elsif options.key?(:file)
- with_fallbacks { find(options[:file], options[:prefix]) }
+ with_fallbacks { find_template(options[:file], options[:prefix]) }
elsif options.key?(:template)
- find(options[:template], options[:prefix])
+ options[:template].respond_to?(:render) ?
+ options[:template] : find_template(options[:template], options[:prefix])
end
end
@@ -73,22 +50,17 @@ module ActionView
# supplied as well.
def _render_template(template, layout = nil, options = {}) #:nodoc:
locals = options[:locals] || {}
- layout = _find_layout(layout) if layout
+ layout = find_layout(layout) if layout
ActiveSupport::Notifications.instrument("action_view.render_template",
- :identifier => template.identifier, :layout => layout.try(:identifier)) do
+ :identifier => template.identifier, :layout => layout.try(:virtual_path)) do
content = template.render(self, locals) { |*name| _layout_for(*name) }
@_content_for[:layout] = content
- if layout
- @_layout = layout.identifier
- content = _render_layout(layout, locals)
- end
-
+ content = _render_layout(layout, locals) if layout
content
end
end
-
end
end