205f8628f0e97e791e24639822342c6470dc2917
[feedcatcher.git] / vendor / rails / actionpack / lib / action_view / template_handlers.rb
1 module ActionView #:nodoc:
2 module TemplateHandlers #:nodoc:
3 autoload :ERB, 'action_view/template_handlers/erb'
4 autoload :RJS, 'action_view/template_handlers/rjs'
5 autoload :Builder, 'action_view/template_handlers/builder'
6
7 def self.extended(base)
8 base.register_default_template_handler :erb, TemplateHandlers::ERB
9 base.register_template_handler :rjs, TemplateHandlers::RJS
10 base.register_template_handler :builder, TemplateHandlers::Builder
11
12 # TODO: Depreciate old template extensions
13 base.register_template_handler :rhtml, TemplateHandlers::ERB
14 base.register_template_handler :rxml, TemplateHandlers::Builder
15 end
16
17 @@template_handlers = {}
18 @@default_template_handlers = nil
19
20 # Register a class that knows how to handle template files with the given
21 # extension. This can be used to implement new template types.
22 # The constructor for the class must take the ActiveView::Base instance
23 # as a parameter, and the class must implement a +render+ method that
24 # takes the contents of the template to render as well as the Hash of
25 # local assigns available to the template. The +render+ method ought to
26 # return the rendered template as a string.
27 def register_template_handler(extension, klass)
28 @@template_handlers[extension.to_sym] = klass
29 end
30
31 def template_handler_extensions
32 @@template_handlers.keys.map(&:to_s).sort
33 end
34
35 def registered_template_handler(extension)
36 extension && @@template_handlers[extension.to_sym]
37 end
38
39 def register_default_template_handler(extension, klass)
40 register_template_handler(extension, klass)
41 @@default_template_handlers = klass
42 end
43
44 def handler_class_for_extension(extension)
45 registered_template_handler(extension) || @@default_template_handlers
46 end
47 end
48 end