2008-02-05 23:26:40 -05:00
|
|
|
module ActionView #:nodoc:
|
2008-07-12 15:33:46 -04:00
|
|
|
class Template
|
2008-12-29 20:27:19 -05:00
|
|
|
class Path
|
|
|
|
attr_reader :path, :paths
|
|
|
|
delegate :hash, :inspect, :to => :path
|
|
|
|
|
|
|
|
def initialize(path)
|
|
|
|
raise ArgumentError, "path already is a Path class" if path.is_a?(Path)
|
|
|
|
@path = path.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
if defined?(RAILS_ROOT)
|
|
|
|
path.to_s.sub(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '')
|
|
|
|
else
|
|
|
|
path.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_str
|
|
|
|
path.to_str
|
|
|
|
end
|
|
|
|
|
|
|
|
def ==(path)
|
|
|
|
to_str == path.to_str
|
|
|
|
end
|
|
|
|
|
|
|
|
def eql?(path)
|
|
|
|
to_str == path.to_str
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a ActionView::Template object for the given path string. The
|
|
|
|
# input path should be relative to the view path directory,
|
|
|
|
# +hello/index.html.erb+. This method also has a special exception to
|
|
|
|
# match partial file names without a handler extension. So
|
|
|
|
# +hello/index.html+ will match the first template it finds with a
|
|
|
|
# known template extension, +hello/index.html.erb+. Template extensions
|
|
|
|
# should not be confused with format extensions +html+, +js+, +xml+,
|
|
|
|
# etc. A format must be supplied to match a formated file. +hello/index+
|
|
|
|
# will never match +hello/index.html.erb+.
|
|
|
|
def [](path)
|
|
|
|
templates_in_path do |template|
|
|
|
|
if template.accessible_paths.include?(path)
|
|
|
|
return template
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def templates_in_path
|
|
|
|
(Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")).each do |file|
|
|
|
|
yield create_template(file) unless File.directory?(file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_template(file)
|
|
|
|
Template.new(file.split("#{self}/").last, self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class EagerPath < Path
|
|
|
|
def initialize(path)
|
|
|
|
super
|
|
|
|
|
|
|
|
@paths = {}
|
|
|
|
templates_in_path do |template|
|
|
|
|
template.load!
|
|
|
|
template.accessible_paths.each do |path|
|
|
|
|
@paths[path] = template
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@paths.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](path)
|
|
|
|
@paths[path]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-03 22:08:27 -04:00
|
|
|
extend TemplateHandlers
|
2008-07-18 16:32:28 -04:00
|
|
|
extend ActiveSupport::Memoizable
|
2008-07-05 13:05:50 -04:00
|
|
|
include Renderable
|
2008-07-03 14:06:00 -04:00
|
|
|
|
2008-12-21 18:23:53 -05:00
|
|
|
# Templates that are exempt from layouts
|
|
|
|
@@exempt_from_layout = Set.new([/\.rjs$/])
|
|
|
|
|
|
|
|
# Don't render layouts for templates with the given extensions.
|
|
|
|
def self.exempt_from_layout(*extensions)
|
|
|
|
regexps = extensions.collect do |extension|
|
|
|
|
extension.is_a?(Regexp) ? extension : /\.#{Regexp.escape(extension.to_s)}$/
|
|
|
|
end
|
|
|
|
@@exempt_from_layout.merge(regexps)
|
|
|
|
end
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
attr_accessor :filename, :load_path, :base_path, :name, :format, :extension
|
|
|
|
delegate :to_s, :to => :path
|
2008-02-05 23:26:40 -05:00
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def initialize(template_path, load_paths = [])
|
|
|
|
template_path = template_path.dup
|
2008-12-03 14:58:47 -05:00
|
|
|
@load_path, @filename = find_full_path(template_path, load_paths)
|
2008-07-12 15:33:46 -04:00
|
|
|
@base_path, @name, @format, @extension = split(template_path)
|
|
|
|
@base_path.to_s.gsub!(/\/$/, '') # Push to split method
|
|
|
|
|
|
|
|
# Extend with partial super powers
|
|
|
|
extend RenderablePartial if @name =~ /^_/
|
|
|
|
end
|
|
|
|
|
2008-12-21 18:23:53 -05:00
|
|
|
def accessible_paths
|
|
|
|
paths = []
|
|
|
|
paths << path
|
|
|
|
paths << path_without_extension
|
|
|
|
if multipart?
|
|
|
|
formats = format.split(".")
|
|
|
|
paths << "#{path_without_format_and_extension}.#{formats.first}"
|
|
|
|
paths << "#{path_without_format_and_extension}.#{formats.second}"
|
|
|
|
end
|
|
|
|
paths
|
|
|
|
end
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def format_and_extension
|
2008-07-14 20:51:43 -04:00
|
|
|
(extensions = [format, extension].compact.join(".")).blank? ? nil : extensions
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
2008-07-14 21:02:59 -04:00
|
|
|
memoize :format_and_extension
|
2008-07-12 15:33:46 -04:00
|
|
|
|
2008-08-06 21:08:27 -04:00
|
|
|
def multipart?
|
|
|
|
format && format.include?('.')
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_type
|
|
|
|
format.gsub('.', '/')
|
|
|
|
end
|
|
|
|
|
2008-07-18 21:14:12 -04:00
|
|
|
def mime_type
|
|
|
|
Mime::Type.lookup_by_extension(format) if format
|
|
|
|
end
|
|
|
|
memoize :mime_type
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def path
|
2008-07-14 20:51:43 -04:00
|
|
|
[base_path, [name, format, extension].compact.join('.')].compact.join('/')
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
2008-07-14 21:02:59 -04:00
|
|
|
memoize :path
|
2008-02-05 23:26:40 -05:00
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def path_without_extension
|
2008-07-14 20:51:43 -04:00
|
|
|
[base_path, [name, format].compact.join('.')].compact.join('/')
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
2008-07-14 21:02:59 -04:00
|
|
|
memoize :path_without_extension
|
2008-07-03 14:06:00 -04:00
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def path_without_format_and_extension
|
2008-07-14 20:51:43 -04:00
|
|
|
[base_path, name].compact.join('/')
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
2008-07-14 21:02:59 -04:00
|
|
|
memoize :path_without_format_and_extension
|
2008-06-11 23:05:36 -04:00
|
|
|
|
2008-09-20 23:57:45 -04:00
|
|
|
def relative_path
|
|
|
|
path = File.expand_path(filename)
|
|
|
|
path.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '') if defined?(RAILS_ROOT)
|
|
|
|
path
|
|
|
|
end
|
|
|
|
memoize :relative_path
|
|
|
|
|
2008-12-21 18:23:53 -05:00
|
|
|
def exempt_from_layout?
|
|
|
|
@@exempt_from_layout.any? { |exempted| path =~ exempted }
|
|
|
|
end
|
|
|
|
|
2008-11-28 15:31:54 -05:00
|
|
|
def mtime
|
|
|
|
File.mtime(filename)
|
|
|
|
end
|
|
|
|
memoize :mtime
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def source
|
2008-07-14 20:51:43 -04:00
|
|
|
File.read(filename)
|
2008-03-02 23:01:35 -05:00
|
|
|
end
|
2008-07-14 21:02:59 -04:00
|
|
|
memoize :source
|
2008-05-01 16:27:21 -04:00
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def method_segment
|
2008-09-20 23:57:45 -04:00
|
|
|
relative_path.to_s.gsub(/([^a-zA-Z0-9_])/) { $1.ord }
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
2008-07-14 21:02:59 -04:00
|
|
|
memoize :method_segment
|
2008-07-12 15:33:46 -04:00
|
|
|
|
|
|
|
def render_template(view, local_assigns = {})
|
|
|
|
render(view, local_assigns)
|
2008-05-01 16:27:21 -04:00
|
|
|
rescue Exception => e
|
|
|
|
raise e unless filename
|
|
|
|
if TemplateError === e
|
2008-09-20 23:57:45 -04:00
|
|
|
e.sub_template_of(self)
|
2008-05-01 16:27:21 -04:00
|
|
|
raise e
|
|
|
|
else
|
2008-07-12 15:33:46 -04:00
|
|
|
raise TemplateError.new(self, view.assigns, e)
|
2008-05-01 16:27:21 -04:00
|
|
|
end
|
|
|
|
end
|
2008-11-28 15:31:54 -05:00
|
|
|
|
|
|
|
def stale?
|
|
|
|
File.mtime(filename) > mtime
|
|
|
|
end
|
|
|
|
|
2008-12-29 20:27:19 -05:00
|
|
|
def recompile?
|
|
|
|
!@cached
|
2008-11-28 15:31:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def load!
|
2008-12-29 20:27:19 -05:00
|
|
|
@cached = true
|
2008-11-28 15:31:54 -05:00
|
|
|
freeze
|
|
|
|
end
|
2008-06-11 23:05:36 -04:00
|
|
|
|
2008-02-05 23:26:40 -05:00
|
|
|
private
|
2008-12-19 08:05:45 -05:00
|
|
|
def valid_extension?(extension)
|
2009-01-16 23:36:50 -05:00
|
|
|
!Template.registered_template_handler(extension).nil?
|
2008-12-19 08:05:45 -05:00
|
|
|
end
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def find_full_path(path, load_paths)
|
|
|
|
load_paths = Array(load_paths) + [nil]
|
|
|
|
load_paths.each do |load_path|
|
2008-12-11 11:17:29 -05:00
|
|
|
file = load_path ? "#{load_path.to_str}/#{path}" : path
|
2008-07-23 11:24:47 -04:00
|
|
|
return load_path, file if File.file?(file)
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
raise MissingTemplate.new(load_paths, path)
|
2008-06-11 23:05:36 -04:00
|
|
|
end
|
2008-07-05 17:27:43 -04:00
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
# Returns file split into an array
|
|
|
|
# [base_path, name, format, extension]
|
|
|
|
def split(file)
|
|
|
|
if m = file.match(/^(.*\/)?([^\.]+)\.?(\w+)?\.?(\w+)?\.?(\w+)?$/)
|
2008-12-19 08:05:45 -05:00
|
|
|
if valid_extension?(m[5]) # Multipart formats
|
2008-07-12 15:33:46 -04:00
|
|
|
[m[1], m[2], "#{m[3]}.#{m[4]}", m[5]]
|
2008-12-19 08:05:45 -05:00
|
|
|
elsif valid_extension?(m[4]) # Single format
|
2008-07-12 15:33:46 -04:00
|
|
|
[m[1], m[2], m[3], m[4]]
|
2008-12-19 08:05:45 -05:00
|
|
|
elsif valid_extension?(m[3]) # No format
|
2008-12-03 10:59:04 -05:00
|
|
|
[m[1], m[2], nil, m[3]]
|
|
|
|
else # No extension
|
|
|
|
[m[1], m[2], m[3], nil]
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
end
|
2008-07-05 17:27:43 -04:00
|
|
|
end
|
2008-02-05 23:26:40 -05:00
|
|
|
end
|
|
|
|
end
|