Extracted Template rendering logic into Renderer module

This commit is contained in:
Joshua Peek
2008-07-03 13:06:00 -05:00
parent 7d5c8505f5
commit 8a442e0d57
5 changed files with 42 additions and 33 deletions

View File

@@ -24,6 +24,8 @@
require 'action_view/template_handlers'
require 'action_view/template_file'
require 'action_view/view_load_paths'
require 'action_view/renderer'
require 'action_view/template'
require 'action_view/partial_template'
require 'action_view/inline_template'

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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