0
module ActionView #:nodoc:
0
- class Template
#:nodoc:0
extend TemplateHandlers
0
- attr_reader :path, :extension
0
+ attr_accessor :filename, :load_path, :base_path, :name, :format, :extension
0
+ delegate :to_s, :to => :path
0
- def initialize(view, path, use_full_path = nil, locals = {})
0
- unless use_full_path == nil
0
- ActiveSupport::Deprecation.warn("use_full_path option has been deprecated and has no affect.", caller)
0
+ def initialize(template_path, load_paths = [])
0
+ template_path = template_path.dup
0
+ @base_path, @name, @format, @extension = split(template_path)
0
+ @base_path.to_s.gsub!(/\/$/, '') # Push to split method
0
+ @load_path, @filename = find_full_path(template_path, load_paths)
0
+ # Extend with partial super powers
0
+ extend RenderablePartial if @name =~ /^_/
0
+ # Eager load memoized methods
0
+ path_without_extension
0
+ path_without_format_and_extension
0
+ # Eager load memoized methods from Renderable
0
+ instance_variables.each { |ivar| ivar.freeze }
0
- @paths = view.view_paths
0
+ def format_and_extension
0
+ @format_and_extension ||= (extensions = [format, extension].compact.join(".")).blank? ? nil : extensions
0
+ @path ||= [base_path, [name, format, extension].compact.join('.')].compact.join('/')
0
- @path = TemplateFile.from_path(path)
0
- @view.first_render ||= @path.to_s
0
+ def path_without_extension
0
+ @path_without_extension ||= [base_path, [name, format].compact.join('.')].compact.join('/')
0
- set_extension_and_file_name
0
+ def path_without_format_and_extension
0
+ @path_without_format_and_extension ||= [base_path, name].compact.join('/')
0
- @method_segment = compiled_method_name_file_path_segment
0
- @locals = (locals && locals.dup) || {}
0
- @handler = self.class.handler_class_for_extension(@extension).new(@view)
0
+ @source ||= File.read(@filename)
0
+ unless @method_segment
0
+ segment = File.expand_path(@filename)
0
+ segment.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}/, '') if defined?(RAILS_ROOT)
0
+ segment.gsub!(/([^a-zA-Z0-9_])/) { $1.ord }
0
+ @method_segment = segment
0
+ def render_template(view, local_assigns = {})
0
+ render(view, local_assigns)
0
raise e unless filename
0
e.sub_template_of(filename)
0
- raise TemplateError.new(self,
@view.assigns, e)
0
+ raise TemplateError.new(self,
view.assigns, e)
0
- @source ||= File.read(@filename)
0
- def base_path_for_exception
0
- (@paths.find_load_path_for_path(@path) || @paths.first).to_s
0
- def set_extension_and_file_name
0
- @extension = @path.extension
0
- @path = @view.send(:template_file_from_name, @path)
0
- raise_missing_template_exception unless @path
0
- @extension = @path.extension
0
- if p = @paths.find_template_file_for_path(path)
0
- @filename = @path.full_path
0
- @extension = @path.extension
0
- raise_missing_template_exception if @filename.blank?
0
- @filename = @original_path
0
- raise_missing_template_exception unless File.exist?(@filename)
0
+ def valid_extension?(extension)
0
+ Template.template_handler_extensions.include?(extension)
0
- def raise_missing_template_exception
0
- full_template_path = @original_path.include?('.') ? @original_path : "#{@original_path}.#{@view.template_format}.erb"
0
- display_paths = @paths.join(':')
0
- template_type = (@original_path =~ /layouts/i) ? 'layout' : 'template'
0
- raise MissingTemplate, "Missing #{template_type} #{full_template_path} in view path #{display_paths}"
0
+ def find_full_path(path, load_paths)
0
+ load_paths = Array(load_paths) + [nil]
0
+ load_paths.each do |load_path|
0
+ file = [load_path, path].compact.join('/')
0
+ return load_path, file if File.exist?(file)
0
+ raise MissingTemplate.new(load_paths, path)
0
- def compiled_method_name_file_path_segment
0
- s = File.expand_path(@filename)
0
- s.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}/, '') if defined?(RAILS_ROOT)
0
- s.gsub!(/([^a-zA-Z0-9_])/) { $1.ord }
0
+ # Returns file split into an array
0
+ # [base_path, name, format, extension]