2008-02-05 23:26:40 -05:00
|
|
|
module ActionView #:nodoc:
|
|
|
|
class Template #:nodoc:
|
2008-07-03 22:08:27 -04:00
|
|
|
extend TemplateHandlers
|
2008-07-03 14:06:00 -04:00
|
|
|
include Renderer
|
|
|
|
|
|
|
|
attr_reader :path, :extension
|
2008-02-05 23:26:40 -05:00
|
|
|
|
2008-07-02 22:38:58 -04:00
|
|
|
def initialize(view, path, use_full_path = nil, locals = {})
|
|
|
|
unless use_full_path == nil
|
|
|
|
ActiveSupport::Deprecation.warn("use_full_path option has been deprecated and has no affect.", caller)
|
|
|
|
end
|
|
|
|
|
2008-02-05 23:26:40 -05:00
|
|
|
@view = view
|
2008-06-15 22:22:27 -04:00
|
|
|
@paths = view.view_paths
|
2008-02-05 23:26:40 -05:00
|
|
|
|
2008-06-15 22:22:27 -04:00
|
|
|
@original_path = path
|
2008-07-02 22:38:58 -04:00
|
|
|
@path = TemplateFile.from_path(path)
|
2008-06-15 22:22:27 -04:00
|
|
|
@view.first_render ||= @path.to_s
|
2008-07-03 14:06:00 -04:00
|
|
|
|
2008-07-02 22:38:58 -04:00
|
|
|
set_extension_and_file_name
|
2008-06-11 23:05:36 -04:00
|
|
|
|
2008-07-03 14:06:00 -04:00
|
|
|
@method_key = @filename
|
2008-02-05 23:26:40 -05:00
|
|
|
@locals = locals || {}
|
2008-07-03 22:08:27 -04:00
|
|
|
@handler = self.class.handler_class_for_extension(@extension).new(@view)
|
2008-03-02 23:01:35 -05:00
|
|
|
end
|
2008-05-01 16:27:21 -04:00
|
|
|
|
|
|
|
def render_template
|
|
|
|
render
|
|
|
|
rescue Exception => e
|
|
|
|
raise e unless filename
|
|
|
|
if TemplateError === e
|
|
|
|
e.sub_template_of(filename)
|
|
|
|
raise e
|
|
|
|
else
|
|
|
|
raise TemplateError.new(self, @view.assigns, e)
|
|
|
|
end
|
|
|
|
end
|
2008-06-11 23:05:36 -04:00
|
|
|
|
2008-02-05 23:26:40 -05:00
|
|
|
def source
|
|
|
|
@source ||= File.read(self.filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_path_for_exception
|
2008-06-15 22:22:27 -04:00
|
|
|
(@paths.find_load_path_for_path(@path) || @paths.first).to_s
|
2008-02-05 23:26:40 -05:00
|
|
|
end
|
2008-06-11 23:05:36 -04:00
|
|
|
|
2008-02-05 23:26:40 -05:00
|
|
|
private
|
2008-07-02 22:38:58 -04:00
|
|
|
def set_extension_and_file_name
|
2008-06-15 22:22:27 -04:00
|
|
|
@extension = @path.extension
|
|
|
|
|
2008-07-02 22:38:58 -04:00
|
|
|
unless @extension
|
|
|
|
@path = @view.send(:template_file_from_name, @path)
|
|
|
|
raise_missing_template_exception unless @path
|
|
|
|
@extension = @path.extension
|
2008-02-05 23:26:40 -05:00
|
|
|
end
|
|
|
|
|
2008-07-02 22:38:58 -04:00
|
|
|
if p = @paths.find_template_file_for_path(path)
|
|
|
|
@path = p
|
|
|
|
@filename = @path.full_path
|
|
|
|
@extension = @path.extension
|
|
|
|
raise_missing_template_exception if @filename.blank?
|
|
|
|
else
|
|
|
|
@filename = @original_path
|
|
|
|
raise_missing_template_exception unless File.exist?(@filename)
|
|
|
|
end
|
2008-06-11 23:05:36 -04:00
|
|
|
end
|
2008-03-04 21:03:24 -05:00
|
|
|
|
2008-06-11 23:05:36 -04:00
|
|
|
def raise_missing_template_exception
|
2008-06-15 22:22:27 -04:00
|
|
|
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'
|
2008-07-03 14:06:00 -04:00
|
|
|
raise MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}"
|
2008-06-11 23:05:36 -04:00
|
|
|
end
|
2008-02-05 23:26:40 -05:00
|
|
|
end
|
|
|
|
end
|