Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/lib/action_view/template.rb

Revision 8981, 3.9 kB (checked in by pratik, 6 months ago)

Moved template handlers related code from ActionView::Base to ActionView::Template

Line 
1 module ActionView #:nodoc:
2   class Template #:nodoc:
3
4     attr_accessor :locals
5     attr_reader :handler, :path, :source, :extension, :filename, :path_without_extension, :method
6
7     def initialize(view, path_or_source, use_full_path, locals = {}, inline = false, inline_type = nil)
8       @view = view
9       @finder = @view.finder
10
11       unless inline
12         # Clear the forward slash at the beginning if exists
13         @path = use_full_path ? path_or_source.sub(/^\//, '') : path_or_source
14         @view.first_render ||= @path
15         @source = nil # Don't read the source until we know that it is required
16         set_extension_and_file_name(use_full_path)
17       else
18         @source = path_or_source
19         @extension = inline_type
20       end
21       @locals = locals || {}
22       @handler = self.class.handler_class_for_extension(@extension).new(@view)
23     end
24    
25     def render
26       prepare!
27       @handler.render(self)
28     end
29
30     def source
31       @source ||= File.read(self.filename)
32     end
33
34     def method_key
35       @method_key ||= (@filename || @source)
36     end
37
38     def base_path_for_exception
39       @finder.find_base_path_for("#{@path_without_extension}.#{@extension}") || @finder.view_paths.first
40     end
41    
42     def prepare!
43       @view.send :evaluate_assigns
44       @view.current_render_extension = @extension
45      
46       if @handler.compilable?
47         @handler.compile_template(self) # compile the given template, if necessary
48         @method = @view.method_names[method_key] # Set the method name for this template and run it
49       end
50     end
51
52     private
53
54     def set_extension_and_file_name(use_full_path)
55       @path_without_extension, @extension = @finder.path_and_extension(@path)
56       if use_full_path
57         if @extension
58           @filename = @finder.pick_template(@path_without_extension, @extension)
59         else
60           @extension = @finder.pick_template_extension(@path).to_s
61           unless @extension
62             raise ActionViewError, "No template found for #{@path} in #{@finder.view_paths.inspect}"
63           end
64           @filename = @finder.pick_template(@path, @extension)
65           @extension = @extension.gsub(/^.+\./, '') # strip off any formats
66         end
67       else
68         @filename = @path
69       end
70
71       if @filename.blank?
72         raise ActionViewError, "Couldn't find template file for #{@path} in #{@finder.view_paths.inspect}"
73       end
74     end
75
76     # Template Handlers
77    
78     @@template_handlers = HashWithIndifferentAccess.new
79     @@default_template_handlers = nil
80    
81     # Register a class that knows how to handle template files with the given
82     # extension. This can be used to implement new template types.
83     # The constructor for the class must take the ActiveView::Base instance
84     # as a parameter, and the class must implement a #render method that
85     # takes the contents of the template to render as well as the Hash of
86     # local assigns available to the template. The #render method ought to
87     # return the rendered template as a string.
88     def self.register_template_handler(extension, klass)
89       @@template_handlers[extension.to_sym] = klass
90       TemplateFinder.update_extension_cache_for(extension.to_s)
91     end
92
93     def self.template_handler_extensions
94       @@template_handlers.keys.map(&:to_s).sort
95     end
96
97     def self.register_default_template_handler(extension, klass)
98       register_template_handler(extension, klass)
99       @@default_template_handlers = klass
100     end
101
102     def self.handler_class_for_extension(extension)
103       (extension && @@template_handlers[extension.to_sym]) || @@default_template_handlers
104     end
105
106     register_default_template_handler :erb, TemplateHandlers::ERB
107     register_template_handler :rjs, TemplateHandlers::RJS
108     register_template_handler :builder, TemplateHandlers::Builder
109
110     # TODO: Depreciate old template extensions
111     register_template_handler :rhtml, TemplateHandlers::ERB
112     register_template_handler :rxml, TemplateHandlers::Builder
113    
114   end
115 end
Note: See TracBrowser for help on using the browser.