2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:48:35 -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 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
|
|
|
|
2019-02-25 16:18:44 -05:00
|
|
|
prepend_formats(template.format)
|
2012-07-11 04:18:52 -04:00
|
|
|
|
2019-01-28 17:40:30 -05:00
|
|
|
render_template(context, template, options[:layout], options[:locals] || {})
|
2010-10-10 06:34:31 -04:00
|
|
|
end
|
|
|
|
|
2014-07-18 23:37:17 -04:00
|
|
|
private
|
|
|
|
|
2016-09-14 04:57:52 -04:00
|
|
|
# Determine the template to be rendered using the given options.
|
2016-08-06 13:55:02 -04:00
|
|
|
def determine_template(options)
|
|
|
|
keys = options.has_key?(:locals) ? options[:locals].keys : []
|
2010-10-10 05:03:18 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
if options.key?(:body)
|
|
|
|
Template::Text.new(options[:body])
|
|
|
|
elsif options.key?(:plain)
|
|
|
|
Template::Text.new(options[:plain])
|
|
|
|
elsif options.key?(:html)
|
|
|
|
Template::HTML.new(options[:html], formats.first)
|
|
|
|
elsif options.key?(:file)
|
2019-03-18 20:17:11 -04:00
|
|
|
if File.exist?(options[:file])
|
2019-04-01 19:29:03 -04:00
|
|
|
Template::RawFile.new(options[:file])
|
2019-03-18 20:17:11 -04:00
|
|
|
else
|
|
|
|
ActiveSupport::Deprecation.warn "render file: should be given the absolute path to a file"
|
2019-04-01 19:35:07 -04:00
|
|
|
@lookup_context.with_fallbacks.find_template(options[:file], nil, false, keys, @details)
|
2019-03-18 20:17:11 -04:00
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
elsif options.key?(:inline)
|
|
|
|
handler = Template.handler_for_extension(options[:type] || "erb")
|
2019-02-22 14:58:04 -05:00
|
|
|
format = if handler.respond_to?(:default_format)
|
|
|
|
handler.default_format
|
|
|
|
else
|
|
|
|
@lookup_context.formats.first
|
|
|
|
end
|
2019-02-22 20:23:54 -05:00
|
|
|
Template::Inline.new(options[:inline], "inline template", handler, locals: keys, format: format)
|
2016-08-06 13:55:02 -04:00
|
|
|
elsif options.key?(:template)
|
|
|
|
if options[:template].respond_to?(:render)
|
|
|
|
options[:template]
|
|
|
|
else
|
2019-01-28 17:03:40 -05:00
|
|
|
@lookup_context.find_template(options[:template], options[:prefixes], false, keys, @details)
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2012-10-25 17:00:14 -04:00
|
|
|
else
|
2016-05-21 08:49:38 -04:00
|
|
|
raise ArgumentError, "You invoked render but did not give any of :partial, :template, :inline, :file, :plain, :html or :body option."
|
2012-10-25 17:00:14 -04:00
|
|
|
end
|
2010-10-10 05:03:18 -04:00
|
|
|
end
|
|
|
|
|
2016-09-14 04:57:52 -04:00
|
|
|
# Renders the given template. A string representing the layout can be
|
|
|
|
# supplied as well.
|
2019-01-28 17:40:30 -05:00
|
|
|
def render_template(view, template, layout_name, locals)
|
2019-03-24 11:13:20 -04:00
|
|
|
render_with_layout(view, template, layout_name, locals) do |layout|
|
2016-08-06 13:55:02 -04:00
|
|
|
instrument(:template, identifier: template.identifier, layout: layout.try(:virtual_path)) do
|
|
|
|
template.render(view, locals) { |*name| view._layout_for(*name) }
|
|
|
|
end
|
2010-10-10 05:03:18 -04:00
|
|
|
end
|
2010-10-10 06:34:31 -04:00
|
|
|
end
|
|
|
|
|
2019-03-24 11:13:20 -04:00
|
|
|
def render_with_layout(view, template, path, locals)
|
2016-08-06 13:55:02 -04:00
|
|
|
layout = path && find_layout(path, locals.keys, [formats.first])
|
|
|
|
content = yield(layout)
|
2010-10-10 05:03:18 -04:00
|
|
|
|
2019-02-13 20:59:21 -05:00
|
|
|
body = if layout
|
2016-08-06 13:55:02 -04:00
|
|
|
view.view_flow.set(:layout, content)
|
2016-08-16 03:30:11 -04:00
|
|
|
layout.render(view, locals) { |*name| view._layout_for(*name) }
|
2016-08-06 13:55:02 -04:00
|
|
|
else
|
|
|
|
content
|
|
|
|
end
|
2019-02-20 08:20:24 -05:00
|
|
|
build_rendered_template(body, template)
|
2010-10-10 05:03:18 -04:00
|
|
|
end
|
|
|
|
|
2016-09-14 04:57:52 -04:00
|
|
|
# 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.
|
2016-08-06 13:55:02 -04:00
|
|
|
def find_layout(layout, keys, formats)
|
|
|
|
resolve_layout(layout, keys, formats)
|
|
|
|
end
|
2011-12-08 10:37:56 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def resolve_layout(layout, keys, formats)
|
|
|
|
details = @details.dup
|
|
|
|
details[:formats] = formats
|
2014-05-08 21:33:01 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
case layout
|
|
|
|
when String
|
|
|
|
begin
|
|
|
|
if layout.start_with?("/")
|
2019-03-29 14:04:16 -04:00
|
|
|
ActiveSupport::Deprecation.warn "Rendering layouts from an absolute path is deprecated."
|
2019-01-28 17:03:40 -05:00
|
|
|
@lookup_context.with_fallbacks.find_template(layout, nil, false, [], details)
|
2016-08-06 13:55:02 -04:00
|
|
|
else
|
2019-01-28 17:03:40 -05:00
|
|
|
@lookup_context.find_template(layout, nil, false, [], details)
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
|
|
|
rescue ActionView::MissingTemplate
|
|
|
|
all_details = @details.merge(formats: @lookup_context.default_formats)
|
|
|
|
raise unless template_exists?(layout, nil, false, [], all_details)
|
2011-12-08 14:59:43 -05:00
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
when Proc
|
2019-02-22 13:39:25 -05:00
|
|
|
resolve_layout(layout.call(@lookup_context, formats), keys, formats)
|
2016-08-06 13:55:02 -04:00
|
|
|
else
|
|
|
|
layout
|
2010-10-10 05:03:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-10-16 11:39:11 -04:00
|
|
|
end
|