aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/renderer/template_renderer.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-10-10 11:03:18 +0200
committerJosé Valim <jose.valim@gmail.com>2010-10-10 12:43:26 +0200
commitb88f4ca93bcaef9a6bfd21d95acc8f432a3c8e5c (patch)
tree94ca53a9d78f749b18ab6b325afc54a1c991f1ca /actionpack/lib/action_view/renderer/template_renderer.rb
parentab2f2b22a6d0656f719c294d40e35d21c752ba8c (diff)
downloadrails-b88f4ca93bcaef9a6bfd21d95acc8f432a3c8e5c.tar.gz
rails-b88f4ca93bcaef9a6bfd21d95acc8f432a3c8e5c.tar.bz2
rails-b88f4ca93bcaef9a6bfd21d95acc8f432a3c8e5c.zip
Clean up the house before moving in the new furniture.
This commit moves all the template rendering logic that was hanging around AV::Base to renderer objects.
Diffstat (limited to 'actionpack/lib/action_view/renderer/template_renderer.rb')
-rw-r--r--actionpack/lib/action_view/renderer/template_renderer.rb66
1 files changed, 66 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/renderer/template_renderer.rb b/actionpack/lib/action_view/renderer/template_renderer.rb
new file mode 100644
index 0000000000..3acc68dcac
--- /dev/null
+++ b/actionpack/lib/action_view/renderer/template_renderer.rb
@@ -0,0 +1,66 @@
+require 'action_view/renderer/abstract_renderer'
+
+module ActionView
+ class TemplateRenderer < AbstractRenderer #:nodoc:
+ def render(options)
+ wrap_formats(options[:template] || options[:file]) do
+ template = determine_template(options)
+ lookup_context.freeze_formats(template.formats, true)
+ render_template(template, options[:layout], options)
+ end
+ end
+
+ # Determine the template to be rendered using the given options.
+ def determine_template(options) #:nodoc:
+ keys = (options[:locals] ||= {}).keys
+
+ if options.key?(:inline)
+ handler = Template.handler_class_for_extension(options[:type] || "erb")
+ Template.new(options[:inline], "inline template", handler, { :locals => keys })
+ elsif options.key?(:text)
+ Template::Text.new(options[:text], formats.try(:first))
+ elsif options.key?(:file)
+ with_fallbacks { find_template(options[:file], options[:prefix], false, keys) }
+ elsif options.key?(:template)
+ options[:template].respond_to?(:render) ?
+ options[:template] : find_template(options[:template], options[:prefix], false, keys)
+ end
+ end
+
+ # Renders the given template. An string representing the layout can be
+ # supplied as well.
+ def render_template(template, layout = nil, options = {}) #:nodoc:
+ view, locals = @view, options[:locals] || {}
+ layout = find_layout(layout, locals.keys) if layout
+
+ ActiveSupport::Notifications.instrument("render_template.action_view",
+ :identifier => template.identifier, :layout => layout.try(:virtual_path)) do
+
+ content = template.render(view, locals) { |*name| view._layout_for(*name) }
+
+ if layout
+ view.store_content_for(:layout, content)
+ content = render_layout(layout, locals)
+ end
+
+ 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
+end \ No newline at end of file