aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/template.rb
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2008-02-06 04:26:40 +0000
committerMichael Koziarski <michael@koziarski.com>2008-02-06 04:26:40 +0000
commit692dbbf79387b56e241e1acd05f74f7d71ff79a6 (patch)
tree7d1c1c9f7a8baa517b525a0d02821fd47ca34daf /actionpack/lib/action_view/template.rb
parent8bc9018882c55cea06d062a9d2d4a32f92b2dc47 (diff)
downloadrails-692dbbf79387b56e241e1acd05f74f7d71ff79a6.tar.gz
rails-692dbbf79387b56e241e1acd05f74f7d71ff79a6.tar.bz2
rails-692dbbf79387b56e241e1acd05f74f7d71ff79a6.zip
Introduce a Template class to ActionView. Closes #11024 [lifofifo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8805 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/template.rb')
-rw-r--r--actionpack/lib/action_view/template.rb65
1 files changed, 65 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb
new file mode 100644
index 0000000000..a814d7b4e5
--- /dev/null
+++ b/actionpack/lib/action_view/template.rb
@@ -0,0 +1,65 @@
+module ActionView #:nodoc:
+ class Template #:nodoc:
+
+ attr_accessor :locals
+ attr_reader :handler, :path, :source, :extension, :filename, :path_without_extension
+
+ def initialize(view, path_or_source, use_full_path, locals = {}, inline = false, inline_type = nil)
+ @view = view
+ @finder = @view.finder
+
+ unless inline
+ # Clear the forward slash at the beginning if exists
+ @path = use_full_path ? path_or_source.sub(/^\//, '') : path_or_source
+ @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)
+ else
+ @source = path_or_source
+ @extension = inline_type
+ end
+ @locals = locals || {}
+ end
+
+ def source
+ @source ||= File.read(self.filename)
+ end
+
+ def method_key
+ @method_key ||= (@filename || @source)
+ end
+
+ def handler
+ @handler ||= @view.class.handler_class_for_extension(@extension).new(@view)
+ end
+
+ def base_path_for_exception
+ @finder.find_base_path_for("#{@path_without_extension}.#{@extension}") || @finder.view_paths.first
+ end
+
+ private
+
+ 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
+ unless @extension
+ raise ActionViewError, "No template found for #{@path} in #{@finder.view_paths.inspect}"
+ end
+ @filename = @finder.pick_template(@path, @extension)
+ @extension = @extension.gsub(/^.+\./, '') # strip off any formats
+ end
+ else
+ @filename = @path
+ end
+
+ if @filename.blank?
+ raise ActionViewError, "Couldn't find template file for #{@path} in #{@finder.view_paths.inspect}"
+ end
+ end
+
+ end
+end