From 8a442e0d57fabedcaf3ded836cfc12f7067ead68 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 3 Jul 2008 13:06:00 -0500 Subject: Extracted Template rendering logic into Renderer module --- actionpack/lib/action_view/inline_template.rb | 9 ++++--- actionpack/lib/action_view/partial_template.rb | 2 +- actionpack/lib/action_view/renderer.rb | 29 ++++++++++++++++++++++ actionpack/lib/action_view/template.rb | 33 +++++--------------------- 4 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 actionpack/lib/action_view/renderer.rb (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/inline_template.rb b/actionpack/lib/action_view/inline_template.rb index 3a97f47e32..19ab92ce1a 100644 --- a/actionpack/lib/action_view/inline_template.rb +++ b/actionpack/lib/action_view/inline_template.rb @@ -1,5 +1,7 @@ module ActionView #:nodoc: - class InlineTemplate < Template #:nodoc: + class InlineTemplate #:nodoc: + include Renderer + def initialize(view, source, locals = {}, type = nil) @view = view @@ -7,11 +9,8 @@ module ActionView #:nodoc: @extension = type @locals = locals || {} + @method_key = @source @handler = Base.handler_class_for_extension(@extension).new(@view) end - - def method_key - @source - end end end diff --git a/actionpack/lib/action_view/partial_template.rb b/actionpack/lib/action_view/partial_template.rb index 6ebe165a15..72f831e937 100644 --- a/actionpack/lib/action_view/partial_template.rb +++ b/actionpack/lib/action_view/partial_template.rb @@ -18,7 +18,7 @@ module ActionView #:nodoc: def render ActionController::Base.benchmark("Rendered #{@path.path_without_format_and_extension}", Logger::DEBUG, false) do - @handler.render(self) + super end end diff --git a/actionpack/lib/action_view/renderer.rb b/actionpack/lib/action_view/renderer.rb new file mode 100644 index 0000000000..e6c64d2749 --- /dev/null +++ b/actionpack/lib/action_view/renderer.rb @@ -0,0 +1,29 @@ +module ActionView + module Renderer + # TODO: Local assigns should not be tied to template instance + attr_accessor :locals + + # TODO: These readers should be private + attr_reader :filename, :source, :handler, :method_key, :method + + def render + prepare! + @handler.render(self) + end + + private + def prepare! + unless @prepared + @view.send(:evaluate_assigns) + @view.current_render_extension = @extension + + if @handler.compilable? + @handler.compile_template(self) # compile the given template, if necessary + @method = @view.method_names[method_key] # Set the method name for this template and run it + end + + @prepared = true + end + end + end +end diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index d4118f0f86..8142232c8f 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -1,12 +1,13 @@ module ActionView #:nodoc: class Template #:nodoc: + include Renderer + class << self # TODO: Deprecate delegate :register_template_handler, :to => 'ActionView::Base' end - attr_accessor :locals - attr_reader :handler, :path, :extension, :filename, :method + attr_reader :path, :extension def initialize(view, path, use_full_path = nil, locals = {}) unless use_full_path == nil @@ -19,9 +20,10 @@ module ActionView #:nodoc: @original_path = path @path = TemplateFile.from_path(path) @view.first_render ||= @path.to_s - @source = nil # Don't read the source until we know that it is required + set_extension_and_file_name + @method_key = @filename @locals = locals || {} @handler = Base.handler_class_for_extension(@extension).new(@view) end @@ -38,37 +40,14 @@ module ActionView #:nodoc: end end - def render - prepare! - @handler.render(self) - end - - def path_without_extension - @path.path_without_extension - end - def source @source ||= File.read(self.filename) end - def method_key - @filename - end - def base_path_for_exception (@paths.find_load_path_for_path(@path) || @paths.first).to_s end - def prepare! - @view.send :evaluate_assigns - @view.current_render_extension = @extension - - if @handler.compilable? - @handler.compile_template(self) # compile the given template, if necessary - @method = @view.method_names[method_key] # Set the method name for this template and run it - end - end - private def set_extension_and_file_name @extension = @path.extension @@ -94,7 +73,7 @@ module ActionView #:nodoc: full_template_path = @original_path.include?('.') ? @original_path : "#{@original_path}.#{@view.template_format}.erb" display_paths = @paths.join(':') template_type = (@original_path =~ /layouts/i) ? 'layout' : 'template' - raise(MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}") + raise MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}" end end end -- cgit v1.2.3