Lookup a template class for the given filename or file extension. Return nil when no implementation is found.
# File lib/tilt.rb, line 69 def self.[](file) pattern = file.to_s.downcase until pattern.empty? || registered?(pattern) pattern = File.basename(pattern) pattern.sub!(/^[^.]*\.?/, '') end # Try to find a preferred engine. preferred_klass = @preferred_mappings[pattern] return preferred_klass if preferred_klass # Fall back to the general list of mappings. klasses = @template_mappings[pattern] # Try to find an engine which is already loaded. template = klasses.detect do |klass| if klass.respond_to?(:engine_initialized?) klass.engine_initialized? end end return template if template # Try each of the classes until one succeeds. If all of them fails, # we'll raise the error of the first class. first_failure = nil klasses.each do |klass| begin klass.new { '' } rescue Exception => ex first_failure ||= ex next else return klass end end raise first_failure if first_failure end
Hash of template path pattern => template implementation class mappings.
# File lib/tilt.rb, line 8 def self.mappings @template_mappings end
Create a new template for the given file using the file's extension to determine the the template mapping.
# File lib/tilt.rb, line 59 def self.new(file, line=nil, options={}, &block) if template_class = self[file] template_class.new(file, line, options, &block) else fail "No template engine registered for #{File.basename(file)}" end end
# File lib/tilt.rb, line 12 def self.normalize(ext) ext.to_s.downcase.sub(/^\./, '') end
Makes a template class preferred for the given file extensions. If you don't provide any extensions, it will be preferred for all its already registered extensions:
# Prefer RDiscount for its registered file extensions: Tilt.prefer(Tilt::RDiscountTemplate) # Prefer RDiscount only for the .md extensions: Tilt.prefer(Tilt::RDiscountTemplate, '.md')
# File lib/tilt.rb, line 38 def self.prefer(template_class, *extensions) if extensions.empty? mappings.each do |ext, klasses| @preferred_mappings[ext] = template_class if klasses.include? template_class end else extensions.each do |ext| ext = normalize(ext) register(template_class, ext) @preferred_mappings[ext] = template_class end end end
Register a template implementation by file extension.
# File lib/tilt.rb, line 17 def self.register(template_class, *extensions) if template_class.respond_to?(:to_str) # Support register(ext, template_class) too extensions, template_class = [template_class], extensions[0] end extensions.each do |ext| ext = normalize(ext) mappings[ext].unshift(template_class).uniq! end end
Returns true when a template exists on an exact match of the provided file extension
# File lib/tilt.rb, line 53 def self.registered?(ext) mappings.key?(ext.downcase) && !mappings[ext.downcase].empty? end