Class: ActionView::TemplateRenderer
- Inherits:
-
AbstractRenderer
- Object
- AbstractRenderer
- ActionView::TemplateRenderer
- Defined in:
- lib/action_view/renderer/template_renderer.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Method Summary collapse
-
#determine_template(options) ⇒ Object
Determine the template to be rendered using the given options.
-
#find_layout(layout, keys) ⇒ Object
This is the method which actually finds the layout using details in the lookup context object.
- #render(context, options) ⇒ Object
-
#render_template(template, layout_name = nil, locals = nil) ⇒ Object
Renders the given template.
-
#render_with_layout(path, locals) ⇒ Object
:nodoc:.
- #resolve_layout(layout, keys) ⇒ Object
Methods inherited from AbstractRenderer
Constructor Details
This class inherits a constructor from ActionView::AbstractRenderer
Instance Method Details
#determine_template(options) ⇒ Object
Determine the template to be rendered using the given options.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/action_view/renderer/template_renderer.rb', line 21 def determine_template() #:nodoc: keys = .fetch(:locals, {}).keys if .key?(:text) Template::Text.new([:text], formats.first) elsif .key?(:file) with_fallbacks { find_template([:file], nil, false, keys, @details) } elsif .key?(:inline) handler = Template.handler_for_extension([:type] || "erb") Template.new([:inline], "inline template", handler, :locals => keys) elsif .key?(:template) if [:template].respond_to?(:render) [:template] else find_template([:template], [:prefixes], false, keys, @details) end else raise ArgumentError, "You invoked render but did not give any of :partial, :template, :inline, :file or :text option." end end |
#find_layout(layout, keys) ⇒ Object
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.
70 71 72 |
# File 'lib/action_view/renderer/template_renderer.rb', line 70 def find_layout(layout, keys) with_layout_format { resolve_layout(layout, keys) } end |
#render(context, options) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/action_view/renderer/template_renderer.rb', line 5 def render(context, ) @view = context @details = extract_details() template = determine_template() context = @lookup_context prepend_formats(template.formats) unless context.rendered_format context.rendered_format = template.formats.first || formats.first end render_template(template, [:layout], [:locals]) end |
#render_template(template, layout_name = nil, locals = nil) ⇒ Object
Renders the given template. A string representing the layout can be supplied as well.
44 45 46 47 48 49 50 51 52 |
# File 'lib/action_view/renderer/template_renderer.rb', line 44 def render_template(template, layout_name = nil, locals = nil) #:nodoc: view, locals = @view, locals || {} 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) } end end end |
#render_with_layout(path, locals) ⇒ Object
:nodoc:
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/action_view/renderer/template_renderer.rb', line 54 def render_with_layout(path, locals) #:nodoc: layout = path && find_layout(path, locals.keys) content = yield(layout) if layout view = @view view.view_flow.set(:layout, content) layout.render(view, locals){ |*name| view._layout_for(*name) } else content end end |
#resolve_layout(layout, keys) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/action_view/renderer/template_renderer.rb', line 74 def resolve_layout(layout, keys) case layout when String 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) end when Proc resolve_layout(layout.call, keys) when FalseClass nil else layout end end |