2008-02-05 23:26:40 -05:00
|
|
|
module ActionView #:nodoc:
|
|
|
|
class Template #:nodoc:
|
2008-06-11 23:05:36 -04:00
|
|
|
extend TemplateHandlers
|
2008-02-05 23:26:40 -05:00
|
|
|
|
|
|
|
attr_accessor :locals
|
2008-04-19 11:16:32 -04:00
|
|
|
attr_reader :handler, :path, :extension, :filename, :path_without_extension, :method
|
2008-02-05 23:26:40 -05:00
|
|
|
|
2008-04-19 11:16:32 -04:00
|
|
|
def initialize(view, path, use_full_path, locals = {})
|
2008-02-05 23:26:40 -05:00
|
|
|
@view = view
|
|
|
|
@finder = @view.finder
|
|
|
|
|
2008-04-19 11:16:32 -04:00
|
|
|
# Clear the forward slash at the beginning if exists
|
|
|
|
@path = use_full_path ? path.sub(/^\//, '') : path
|
|
|
|
@view.first_render ||= @path
|
|
|
|
@source = nil # Don't read the source until we know that it is required
|
|
|
|
set_extension_and_file_name(use_full_path)
|
2008-06-11 23:05:36 -04:00
|
|
|
|
2008-02-05 23:26:40 -05:00
|
|
|
@locals = locals || {}
|
2008-03-04 21:03:24 -05: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-03-02 23:01:35 -05:00
|
|
|
def render
|
|
|
|
prepare!
|
|
|
|
@handler.render(self)
|
2008-02-05 23:26:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def source
|
|
|
|
@source ||= File.read(self.filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_key
|
2008-04-19 11:16:32 -04:00
|
|
|
@filename
|
2008-02-05 23:26:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def base_path_for_exception
|
|
|
|
@finder.find_base_path_for("#{@path_without_extension}.#{@extension}") || @finder.view_paths.first
|
|
|
|
end
|
2008-06-11 23:05:36 -04:00
|
|
|
|
2008-03-02 23:01:35 -05:00
|
|
|
def prepare!
|
|
|
|
@view.send :evaluate_assigns
|
|
|
|
@view.current_render_extension = @extension
|
2008-06-11 23:05:36 -04:00
|
|
|
|
2008-03-02 23:01:35 -05:00
|
|
|
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
|
2008-02-05 23:26:40 -05:00
|
|
|
|
|
|
|
private
|
2008-06-11 23:05:36 -04:00
|
|
|
def set_extension_and_file_name(use_full_path)
|
|
|
|
@path_without_extension, @extension = @finder.path_and_extension(@path)
|
|
|
|
if use_full_path
|
|
|
|
if @extension
|
|
|
|
@filename = @finder.pick_template(@path_without_extension, @extension)
|
|
|
|
else
|
|
|
|
@extension = @finder.pick_template_extension(@path).to_s
|
|
|
|
raise_missing_template_exception unless @extension
|
|
|
|
|
|
|
|
@filename = @finder.pick_template(@path, @extension)
|
|
|
|
@extension = @extension.gsub(/^.+\./, '') # strip off any formats
|
|
|
|
end
|
2008-02-05 23:26:40 -05:00
|
|
|
else
|
2008-06-11 23:05:36 -04:00
|
|
|
@filename = @path
|
2008-02-05 23:26:40 -05:00
|
|
|
end
|
|
|
|
|
2008-06-11 23:05:36 -04:00
|
|
|
raise_missing_template_exception if @filename.blank?
|
|
|
|
end
|
2008-03-04 21:03:24 -05:00
|
|
|
|
2008-06-11 23:05:36 -04:00
|
|
|
def raise_missing_template_exception
|
|
|
|
full_template_path = @path.include?('.') ? @path : "#{@path}.#{@view.template_format}.erb"
|
|
|
|
display_paths = @finder.view_paths.join(':')
|
|
|
|
template_type = (@path =~ /layouts/i) ? 'layout' : 'template'
|
|
|
|
raise(MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}")
|
|
|
|
end
|
2008-02-05 23:26:40 -05:00
|
|
|
end
|
|
|
|
end
|