2010-10-10 06:34:31 -04:00
|
|
|
require 'active_support/core_ext/object/try'
|
2010-10-10 05:03:18 -04:00
|
|
|
|
|
|
|
module ActionView
|
|
|
|
class TemplateRenderer < AbstractRenderer #:nodoc:
|
2011-05-01 06:37:57 -04:00
|
|
|
def render(context, options)
|
2011-09-22 09:03:05 -04:00
|
|
|
@view = context
|
2011-09-22 17:51:44 -04:00
|
|
|
@details = extract_details(options)
|
2011-09-22 09:03:05 -04:00
|
|
|
template = determine_template(options)
|
2012-03-07 08:55:06 -05:00
|
|
|
context = @lookup_context
|
|
|
|
|
|
|
|
unless context.rendered_format
|
2012-03-28 17:52:56 -04:00
|
|
|
context.formats = template.formats unless template.formats.empty?
|
|
|
|
context.rendered_format = context.formats.first
|
2012-03-07 08:55:06 -05:00
|
|
|
end
|
|
|
|
|
2011-09-22 09:03:05 -04:00
|
|
|
render_template(template, options[:layout], options[:locals])
|
2010-10-10 06:34:31 -04:00
|
|
|
end
|
|
|
|
|
2010-10-10 05:03:18 -04:00
|
|
|
# Determine the template to be rendered using the given options.
|
|
|
|
def determine_template(options) #:nodoc:
|
2011-09-22 09:03:05 -04:00
|
|
|
keys = options[:locals].try(:keys) || []
|
2010-10-10 05:03:18 -04:00
|
|
|
|
2010-10-10 06:34:31 -04:00
|
|
|
if options.key?(:text)
|
2010-10-10 05:03:18 -04:00
|
|
|
Template::Text.new(options[:text], formats.try(:first))
|
|
|
|
elsif options.key?(:file)
|
2011-09-22 09:03:05 -04:00
|
|
|
with_fallbacks { find_template(options[:file], nil, false, keys, @details) }
|
2010-10-10 06:34:31 -04:00
|
|
|
elsif options.key?(:inline)
|
2010-12-01 05:22:48 -05:00
|
|
|
handler = Template.handler_for_extension(options[:type] || "erb")
|
2010-10-14 03:47:49 -04:00
|
|
|
Template.new(options[:inline], "inline template", handler, :locals => keys)
|
2010-10-10 05:03:18 -04:00
|
|
|
elsif options.key?(:template)
|
|
|
|
options[:template].respond_to?(:render) ?
|
2011-09-22 09:03:05 -04:00
|
|
|
options[:template] : find_template(options[:template], options[:prefixes], false, keys, @details)
|
2011-12-21 14:31:53 -05:00
|
|
|
else
|
|
|
|
raise ArgumentError, "You invoked render but did not give any of :partial, :template, :inline, :file or :text option."
|
2010-10-10 05:03:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Renders the given template. An string representing the layout can be
|
|
|
|
# supplied as well.
|
2010-10-10 06:34:31 -04:00
|
|
|
def render_template(template, layout_name = nil, locals = {}) #:nodoc:
|
|
|
|
view, locals = @view, locals || {}
|
2010-10-10 05:03:18 -04:00
|
|
|
|
2010-10-10 06:34:31 -04:00
|
|
|
render_with_layout(layout_name, locals) do |layout|
|
|
|
|
instrument(:template, :identifier => template.identifier, :layout => layout.try(:virtual_path)) do
|
|
|
|
template.render(view, locals) { |*name| view._layout_for(*name) }
|
2010-10-10 05:03:18 -04:00
|
|
|
end
|
2010-10-10 06:34:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_with_layout(path, locals) #:nodoc:
|
|
|
|
layout = path && find_layout(path, locals.keys)
|
|
|
|
content = yield(layout)
|
2010-10-10 05:03:18 -04:00
|
|
|
|
2010-10-10 06:34:31 -04:00
|
|
|
if layout
|
|
|
|
view = @view
|
2011-05-01 06:16:31 -04:00
|
|
|
view.view_flow.set(:layout, content)
|
2010-10-10 06:34:31 -04:00
|
|
|
layout.render(view, locals){ |*name| view._layout_for(*name) }
|
|
|
|
else
|
2010-10-10 05:03:18 -04:00
|
|
|
content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This is the method which actually finds the layout using details in the lookup
|
|
|
|
# context object. If no layout is found, it checks if at least a layout with
|
|
|
|
# the given name exists across all details before raising the error.
|
|
|
|
def find_layout(layout, keys)
|
2011-12-08 10:37:56 -05:00
|
|
|
with_layout_format { resolve_layout(layout, keys) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolve_layout(layout, keys)
|
|
|
|
case layout
|
|
|
|
when String
|
2011-12-08 14:59:43 -05:00
|
|
|
begin
|
|
|
|
if layout =~ /^\//
|
|
|
|
with_fallbacks { find_template(layout, nil, false, keys, @details) }
|
|
|
|
else
|
|
|
|
find_template(layout, nil, false, keys, @details)
|
|
|
|
end
|
|
|
|
rescue ActionView::MissingTemplate
|
|
|
|
all_details = @details.merge(:formats => @lookup_context.default_formats)
|
|
|
|
raise unless template_exists?(layout, nil, false, keys, all_details)
|
2010-10-10 05:03:18 -04:00
|
|
|
end
|
2011-12-08 10:37:56 -05:00
|
|
|
when Proc
|
|
|
|
resolve_layout(layout.call, keys)
|
2011-12-08 16:56:50 -05:00
|
|
|
when FalseClass
|
|
|
|
nil
|
2011-12-08 10:37:56 -05:00
|
|
|
else
|
|
|
|
layout
|
2010-10-10 05:03:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-10-16 11:39:11 -04:00
|
|
|
end
|