Class: ActionView::TemplateRenderer
Do not use. This class is for internal use only.
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AbstractRenderer
|
|
Instance Chain:
self,
AbstractRenderer
|
|
Inherits: |
ActionView::AbstractRenderer
|
Defined in: | actionview/lib/action_view/renderer/template_renderer.rb |
Constant Summary
AbstractRenderer
- Inherited
Class Method Summary
AbstractRenderer
- Inherited
Instance Attribute Summary
AbstractRenderer
- Inherited
Instance Method Summary
- #render(context, options)
-
#determine_template(options)
private
Determine the template to be rendered using the given options.
-
#find_layout(layout, keys, formats)
private
This is the method which actually finds the layout using details in the lookup context object.
-
#render_template(view, template, layout_name, locals)
private
Renders the given template.
- #render_with_layout(view, template, path, locals) private
- #resolve_layout(layout, keys, formats) private
AbstractRenderer
- Inherited
Constructor Details
This class inherits a constructor from ActionView::AbstractRenderer
Instance Method Details
#determine_template(options) (private)
Determine the template to be rendered using the given options.
# File 'actionview/lib/action_view/renderer/template_renderer.rb', line 16
def determine_template( ) keys = .has_key?(:locals) ? [:locals].keys : [] if .key?(:body) Template::Text.new( [:body]) elsif .key?(:plain) Template::Text.new( [:plain]) elsif .key?(:html) Template::HTML.new( [:html], formats.first) elsif .key?(:file) if File.exist?( [:file]) Template::RawFile.new( [:file]) else if Pathname.new( [:file]).absolute? raise ArgumentError, "File #{ [:file]} does not exist" else raise ArgumentError, "`render file:` should be given the absolute path to a file. '#{ [:file]}' was given instead" end end elsif .key?(:inline) handler = Template.handler_for_extension( [:type] || "erb") format = if handler.respond_to?(:default_format) handler.default_format else @lookup_context.formats.first end Template::Inline.new( [:inline], "inline template", handler, locals: keys, format: format) elsif .key?(:renderable) Template::Renderable.new( [:renderable]) elsif .key?(:template) if [:template].respond_to?(:render) [:template] else @lookup_context.find_template( [:template], [:prefixes], false, keys, @details) end else raise ArgumentError, "You invoked render but did not give any of :body, :file, :html, :inline, :partial, :plain, :renderable, or :template option." end end
#find_layout(layout, keys, formats) (private)
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.
# File 'actionview/lib/action_view/renderer/template_renderer.rb', line 88
def find_layout(layout, keys, formats) resolve_layout(layout, keys, formats) end
#render(context, options)
[ GitHub ]# File 'actionview/lib/action_view/renderer/template_renderer.rb', line 5
def render(context, ) @details = extract_details( ) template = determine_template( ) prepend_formats(template.format) render_template(context, template, [:layout], [:locals] || {}) end
#render_template(view, template, layout_name, locals) (private)
Renders the given template. A string representing the layout can be supplied as well.
# File 'actionview/lib/action_view/renderer/template_renderer.rb', line 58
def render_template(view, template, layout_name, locals) render_with_layout(view, template, layout_name, locals) do |layout| ActiveSupport::Notifications.instrument( "render_template.action_view", identifier: template.identifier, layout: layout && layout.virtual_path, locals: locals ) do template.render(view, locals) { |*name| view._layout_for(*name) } end end end
#render_with_layout(view, template, path, locals) (private)
[ GitHub ]# File 'actionview/lib/action_view/renderer/template_renderer.rb', line 71
def render_with_layout(view, template, path, locals) layout = path && find_layout(path, locals.keys, [formats.first]) body = if layout ActiveSupport::Notifications.instrument("render_layout.action_view", identifier: layout.identifier) do view.view_flow.set(:layout, yield(layout)) layout.render(view, locals) { |*name| view._layout_for(*name) } end else yield end build_rendered_template(body, template) end
#resolve_layout(layout, keys, formats) (private)
[ GitHub ]# File 'actionview/lib/action_view/renderer/template_renderer.rb', line 92
def resolve_layout(layout, keys, formats) details = @details.dup details[:formats] = formats case layout when String begin if layout.start_with?("/") raise ArgumentError, "Rendering layouts from an absolute path is not supported." else @lookup_context.find_template(layout, nil, false, [], details) end rescue ActionView::MissingTemplate all_details = @details.merge(formats: @lookup_context.default_formats) raise unless template_exists?(layout, nil, false, [], **all_details) end when Proc resolve_layout(layout.call(@lookup_context, formats), keys, formats) else layout end end