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