2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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:
|
2016-08-06 12:48:35 -04:00
|
|
|
autoload :Raw, "action_view/template/handlers/raw"
|
|
|
|
autoload :ERB, "action_view/template/handlers/erb"
|
|
|
|
autoload :Html, "action_view/template/handlers/html"
|
|
|
|
autoload :Builder, "action_view/template/handlers/builder"
|
2009-12-02 23:01:01 -05:00
|
|
|
|
|
|
|
def self.extended(base)
|
2015-01-03 16:00:27 -05:00
|
|
|
base.register_default_template_handler :raw, Raw.new
|
|
|
|
base.register_template_handler :erb, ERB.new
|
2016-01-05 14:48:09 -05:00
|
|
|
base.register_template_handler :html, Html.new
|
2010-10-09 19:14:50 -04:00
|
|
|
base.register_template_handler :builder, Builder.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.
|
2014-11-20 17:54:22 -05:00
|
|
|
# The handler must respond to +:call+, which will be passed the template
|
2012-02-01 19:49:21 -05:00
|
|
|
# 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
|
|
|
|
|
2014-06-09 10:45:46 -04:00
|
|
|
# Opposite to register_template_handler.
|
|
|
|
def unregister_template_handler(*extensions)
|
|
|
|
extensions.each do |extension|
|
|
|
|
handler = @@template_handlers.delete extension.to_sym
|
|
|
|
@@default_template_handlers = nil if @@default_template_handlers == handler
|
|
|
|
end
|
|
|
|
@@template_extensions = nil
|
|
|
|
end
|
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
def template_handler_extensions
|
2014-10-27 12:28:53 -04:00
|
|
|
@@template_handlers.keys.map(&:to_s).sort
|
2009-12-02 23:01:01 -05:00
|
|
|
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
|