1 require 'action_controller/mime_type'
3 module ActionView
#:nodoc:
5 extend TemplateHandlers
6 extend ActiveSupport
::Memoizable
9 attr_accessor
:filename, :load_path, :base_path, :name, :format, :extension
10 delegate
:to_s, :to => :path
12 def initialize(template_path
, load_paths
= [])
13 template_path
= template_path
.dup
14 @base_path, @name, @format, @extension = split(template_path
)
15 @base_path.to_s
.gsub
!(/\/$/, '') # Push to split method
16 @load_path, @filename = find_full_path(template_path
, load_paths
)
18 # Extend with partial super powers
19 extend RenderablePartial
if @name =~
/^_/
22 def format_and_extension
23 (extensions
= [format
, extension
].compact
.join(".")).blank
? ? nil : extensions
25 memoize
:format_and_extension
28 format
&& format
.include?('.')
36 Mime
::Type.lookup_by_extension(format
) if format
41 [base_path
, [name
, format
, extension
].compact
.join('.')].compact
.join('/')
45 def path_without_extension
46 [base_path
, [name
, format
].compact
.join('.')].compact
.join('/')
48 memoize
:path_without_extension
50 def path_without_format_and_extension
51 [base_path
, name
].compact
.join('/')
53 memoize
:path_without_format_and_extension
56 path
= File
.expand_path(filename
)
57 path
.sub
!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '') if defined?(RAILS_ROOT
)
60 memoize
:relative_path
68 relative_path
.to_s
.gsub(/([^a-zA-Z0-9_])/) { $1.ord
}
70 memoize
:method_segment
72 def render_template(view
, local_assigns
= {})
73 render(view
, local_assigns
)
75 raise e
unless filename
76 if TemplateError
=== e
77 e
.sub_template_of(self)
80 raise TemplateError
.new(self, view
.assigns
, e
)
85 def valid_extension
?(extension
)
86 Template
.template_handler_extensions
.include?(extension
)
89 def find_full_path(path
, load_paths
)
90 load_paths
= Array(load_paths
) + [nil]
91 load_paths
.each
do |load_path
|
92 file
= [load_path
, path
].compact
.join('/')
93 return load_path
, file
if File
.file
?(file
)
95 raise MissingTemplate
.new(load_paths
, path
)
98 # Returns file split into an array
99 # [base_path, name, format, extension]
101 if m
= file
.match(/^(.*\/)?([^\
.]+)\
.?(\w
+)?\
.?(\w
+)?\
.?(\w
+)?$/)
102 if m
[5] # Multipart formats
103 [m
[1], m
[2], "#{m[3]}.#{m[4]}", m
[5]]
104 elsif m
[4] # Single format
105 [m
[1], m
[2], m
[3], m
[4]]
107 if valid_extension
?(m
[3]) # No format
108 [m
[1], m
[2], nil, m
[3]]
110 [m
[1], m
[2], m
[3], nil]