template_renderer.rb 2.6 KB
Newer Older
J
José Valim 已提交
1 2
require 'active_support/core_ext/object/try'
require 'active_support/core_ext/array/wrap'
3 4 5

module ActionView
  class TemplateRenderer < AbstractRenderer #:nodoc:
6 7 8
    def render(context, options)
      @view = context

9 10
      wrap_formats(options[:template] || options[:file]) do
        template = determine_template(options)
J
José Valim 已提交
11
        freeze_formats(template.formats, true)
J
José Valim 已提交
12 13 14 15
        render_template(template, options[:layout], options[:locals])
      end
    end

16 17
    # Determine the template to be rendered using the given options.
    def determine_template(options) #:nodoc:
J
José Valim 已提交
18
      keys = options[:locals].try(:keys) || []
19

J
José Valim 已提交
20
      if options.key?(:text)
21 22
        Template::Text.new(options[:text], formats.try(:first))
      elsif options.key?(:file)
23
        with_fallbacks { find_template(options[:file], options[:prefixes], false, keys) }
J
José Valim 已提交
24
      elsif options.key?(:inline)
25
        handler = Template.handler_for_extension(options[:type] || "erb")
26
        Template.new(options[:inline], "inline template", handler, :locals => keys)
27 28
      elsif options.key?(:template)
        options[:template].respond_to?(:render) ?
A
artemave 已提交
29
          options[:template] : find_template(options[:template], options[:prefixes], false, keys)
30 31 32 33 34
      end
    end

    # Renders the given template. An string representing the layout can be
    # supplied as well.
J
José Valim 已提交
35 36
    def render_template(template, layout_name = nil, locals = {}) #:nodoc:
      view, locals = @view, locals || {}
37

J
José Valim 已提交
38 39 40
      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) }
41
        end
J
José Valim 已提交
42 43 44 45 46 47
      end
    end

    def render_with_layout(path, locals) #:nodoc:
      layout  = path && find_layout(path, locals.keys)
      content = yield(layout)
48

J
José Valim 已提交
49 50
      if layout
        view = @view
51
        view.view_flow.set(:layout, content)
J
José Valim 已提交
52 53
        layout.render(view, locals){ |*name| view._layout_for(*name) }
      else
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        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)
      begin
        with_layout_format do
          layout =~ /^\// ?
            with_fallbacks { find_template(layout, nil, false, keys) } : find_template(layout, nil, false, keys)
        end
      rescue ActionView::MissingTemplate => e
        update_details(:formats => nil) do
          raise unless template_exists?(layout)
        end
      end
    end
  end
74
end