2008-05-18 06:22:34 +02:00
|
|
|
module ActionView #:nodoc:
|
2008-10-27 07:47:01 +01:00
|
|
|
class Template
|
2009-02-04 21:26:08 +01: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)
|
2009-02-28 02:23:00 +01:00
|
|
|
@path = (path.ends_with?(File::SEPARATOR) ? path.to(-2) : path).freeze
|
2009-02-04 21:26:08 +01:00
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
2009-02-28 02:23:00 +01:00
|
|
|
def load!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.new_and_loaded(path)
|
|
|
|
returning new(path) do |path|
|
|
|
|
path.load!
|
2009-02-04 21:26:08 +01:00
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
2009-02-04 21:26:08 +01:00
|
|
|
|
2009-02-28 02:23:00 +01:00
|
|
|
private
|
|
|
|
def relative_path_for_template_file(full_file_path)
|
|
|
|
full_file_path.split("#{@path}/").last
|
2009-02-04 21:26:08 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class EagerPath < Path
|
2009-02-28 02:23:00 +01:00
|
|
|
def load!
|
|
|
|
return if @loaded
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
@paths = {}
|
|
|
|
templates_in_path do |template|
|
|
|
|
template.load!
|
|
|
|
template.accessible_paths.each do |path|
|
|
|
|
@paths[path] = template
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@paths.freeze
|
2009-02-28 02:23:00 +01:00
|
|
|
@loaded = true
|
2009-02-04 21:26:08 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def [](path)
|
2009-02-28 02:23:00 +01:00
|
|
|
load! unless @loaded
|
2009-02-04 21:26:08 +01:00
|
|
|
@paths[path]
|
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
|
|
|
|
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(relative_path_for_template_file(file), self)
|
|
|
|
end
|
2009-02-04 21:26:08 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
extend TemplateHandlers
|
|
|
|
extend ActiveSupport::Memoizable
|
|
|
|
include Renderable
|
|
|
|
|
2009-02-04 21:26:08 +01: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
|
|
|
|
|
2009-02-28 02:23:00 +01:00
|
|
|
attr_accessor :template_path, :filename, :load_path, :base_path
|
2009-02-04 21:26:08 +01:00
|
|
|
attr_accessor :locale, :name, :format, :extension
|
2008-10-27 07:47:01 +01:00
|
|
|
delegate :to_s, :to => :path
|
|
|
|
|
2009-02-28 02:23:00 +01:00
|
|
|
def initialize(template_path, load_path)
|
|
|
|
@template_path = template_path.dup
|
|
|
|
@load_path, @filename = load_path, File.join(load_path, template_path)
|
2009-02-04 21:26:08 +01:00
|
|
|
@base_path, @name, @locale, @format, @extension = split(template_path)
|
|
|
|
@base_path.to_s.gsub!(/\/$/, '') # Push to split method
|
2008-10-27 07:47:01 +01:00
|
|
|
|
|
|
|
# Extend with partial super powers
|
|
|
|
extend RenderablePartial if @name =~ /^_/
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def accessible_paths
|
|
|
|
paths = []
|
2009-02-28 02:23:00 +01:00
|
|
|
|
|
|
|
if valid_extension?(extension)
|
|
|
|
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
|
|
|
|
else
|
|
|
|
# template without explicit template handler should only be reachable through its exact path
|
|
|
|
paths << template_path
|
2009-02-04 21:26:08 +01:00
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
paths
|
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def format_and_extension
|
|
|
|
(extensions = [format, extension].compact.join(".")).blank? ? nil : extensions
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
memoize :format_and_extension
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def multipart?
|
|
|
|
format && format.include?('.')
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def content_type
|
|
|
|
format.gsub('.', '/')
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def mime_type
|
2009-02-04 21:26:08 +01:00
|
|
|
Mime::Type.lookup_by_extension(format) if format && defined?(::Mime)
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
memoize :mime_type
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def path
|
2009-02-04 21:26:08 +01:00
|
|
|
[base_path, [name, locale, format, extension].compact.join('.')].compact.join('/')
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
|
|
|
memoize :path
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def path_without_extension
|
2009-02-04 21:26:08 +01:00
|
|
|
[base_path, [name, locale, format].compact.join('.')].compact.join('/')
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
memoize :path_without_extension
|
|
|
|
|
|
|
|
def path_without_format_and_extension
|
2009-02-04 21:26:08 +01:00
|
|
|
[base_path, [name, locale].compact.join('.')].compact.join('/')
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
memoize :path_without_format_and_extension
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def relative_path
|
|
|
|
path = File.expand_path(filename)
|
|
|
|
path.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '') if defined?(RAILS_ROOT)
|
|
|
|
path
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
memoize :relative_path
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def exempt_from_layout?
|
|
|
|
@@exempt_from_layout.any? { |exempted| path =~ exempted }
|
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def source
|
|
|
|
File.read(filename)
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
memoize :source
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def method_segment
|
|
|
|
relative_path.to_s.gsub(/([^a-zA-Z0-9_])/) { $1.ord }
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
2008-10-27 07:47:01 +01:00
|
|
|
memoize :method_segment
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def render_template(view, local_assigns = {})
|
|
|
|
render(view, local_assigns)
|
|
|
|
rescue Exception => e
|
|
|
|
raise e unless filename
|
|
|
|
if TemplateError === e
|
|
|
|
e.sub_template_of(self)
|
|
|
|
raise e
|
|
|
|
else
|
|
|
|
raise TemplateError.new(self, view.assigns, e)
|
|
|
|
end
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
def load!
|
|
|
|
freeze
|
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
private
|
|
|
|
def valid_extension?(extension)
|
2009-02-04 21:26:08 +01:00
|
|
|
!Template.registered_template_handler(extension).nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_locale?(locale)
|
|
|
|
I18n.available_locales.include?(locale.to_sym)
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns file split into an array
|
2009-02-04 21:26:08 +01:00
|
|
|
# [base_path, name, locale, format, extension]
|
2008-10-27 07:47:01 +01:00
|
|
|
def split(file)
|
2009-03-16 15:55:30 +01:00
|
|
|
if m = file.to_s.match(/^(.*\/)?([^\.]+)\.(.*)$/)
|
2009-02-04 21:26:08 +01:00
|
|
|
base_path = m[1]
|
|
|
|
name = m[2]
|
|
|
|
extensions = m[3]
|
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
locale = nil
|
|
|
|
format = nil
|
|
|
|
extension = nil
|
|
|
|
|
2009-02-28 02:23:00 +01:00
|
|
|
if m = extensions.split(".")
|
|
|
|
if valid_locale?(m[0]) && m[1] && valid_extension?(m[2]) # All three
|
|
|
|
locale = m[0]
|
2009-02-04 21:26:08 +01:00
|
|
|
format = m[1]
|
|
|
|
extension = m[2]
|
2009-02-28 02:23:00 +01:00
|
|
|
elsif m[0] && m[1] && valid_extension?(m[2]) # Multipart formats
|
|
|
|
format = "#{m[0]}.#{m[1]}"
|
|
|
|
extension = m[2]
|
|
|
|
elsif valid_locale?(m[0]) && valid_extension?(m[1]) # locale and extension
|
|
|
|
locale = m[0]
|
2009-02-04 21:26:08 +01:00
|
|
|
extension = m[1]
|
2009-02-28 02:23:00 +01:00
|
|
|
elsif valid_extension?(m[1]) # format and extension
|
|
|
|
format = m[0]
|
|
|
|
extension = m[1]
|
|
|
|
elsif valid_extension?(m[0]) # Just extension
|
|
|
|
extension = m[0]
|
2009-02-04 21:26:08 +01:00
|
|
|
else # No extension
|
2009-02-28 02:23:00 +01:00
|
|
|
format = m[0]
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
|
|
|
end
|
2009-02-04 21:26:08 +01:00
|
|
|
|
|
|
|
[base_path, name, locale, format, extension]
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
2008-05-18 06:22:34 +02:00
|
|
|
end
|
|
|
|
end
|