2008-06-11 23:22:05 -04:00
|
|
|
module ActionView #:nodoc:
|
2010-06-20 16:26:31 -04:00
|
|
|
# = Action View Template Handlers
|
2009-12-02 23:01:01 -05:00
|
|
|
class Template
|
|
|
|
module Handlers #:nodoc:
|
|
|
|
autoload :ERB, 'action_view/template/handlers/erb'
|
|
|
|
autoload :Builder, 'action_view/template/handlers/builder'
|
2012-05-13 20:22:29 -04:00
|
|
|
autoload :Raw, 'action_view/template/handlers/raw'
|
2009-12-02 23:01:01 -05:00
|
|
|
|
|
|
|
def self.extended(base)
|
2010-10-09 19:14:50 -04:00
|
|
|
base.register_default_template_handler :erb, ERB.new
|
|
|
|
base.register_template_handler :builder, Builder.new
|
2012-05-13 20:22:29 -04:00
|
|
|
base.register_template_handler :raw, Raw.new
|
2012-09-17 11:07:08 -04:00
|
|
|
base.register_template_handler :ruby, :source.to_proc
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@@template_handlers = {}
|
|
|
|
@@default_template_handlers = nil
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
def self.extensions
|
2010-06-04 20:28:04 -04:00
|
|
|
@@template_extensions ||= @@template_handlers.keys
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
2012-02-01 19:49:21 -05:00
|
|
|
# Register an object that knows how to handle template files with the given
|
2012-09-14 11:25:07 -04:00
|
|
|
# extensions. This can be used to implement new template types.
|
2012-02-01 19:49:21 -05:00
|
|
|
# The handler must respond to `:call`, which will be passed the template
|
|
|
|
# and should return the rendered template as a String.
|
2012-09-14 11:25:07 -04:00
|
|
|
def register_template_handler(*extensions, handler)
|
|
|
|
raise(ArgumentError, "Extension is required") if extensions.empty?
|
|
|
|
extensions.each do |extension|
|
|
|
|
@@template_handlers[extension.to_sym] = handler
|
|
|
|
end
|
2012-02-20 14:48:14 -05:00
|
|
|
@@template_extensions = nil
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def template_handler_extensions
|
|
|
|
@@template_handlers.keys.map {|key| key.to_s }.sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def registered_template_handler(extension)
|
|
|
|
extension && @@template_handlers[extension.to_sym]
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_default_template_handler(extension, klass)
|
|
|
|
register_template_handler(extension, klass)
|
|
|
|
@@default_template_handlers = klass
|
|
|
|
end
|
|
|
|
|
2010-12-01 05:22:48 -05:00
|
|
|
def handler_for_extension(extension)
|
|
|
|
registered_template_handler(extension) || @@default_template_handlers
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
2008-06-11 23:22:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|