module Padrino::Rendering::InstanceMethods

def resolve_template(template_path, options={})

Raises:
  • (TemplateNotFound) -

Returns:
  • (Array) -

Options Hash: (**options)
  • :raise_exceptions (Boolean) --
  • :strict_format (Boolean) --

Parameters:
  • options (Hash) --
  • template_path (String) --
def resolve_template(template_path, options={})
  began_at = Time.now
  # Fetch cached template for rendering options
  template_path = template_path.to_s[0] == ?/ ? template_path.to_s : "/#{template_path}"
  rendering_options = [template_path, content_type, locale]
  cached_template = settings.fetch_template_file(rendering_options)
  if cached_template
    logger.debug :cached, began_at, cached_template[0] if settings.logging? && defined?(logger)
    return cached_template
  end
  # Resolve view path and options
  options.reverse_merge!(DEFAULT_RENDERING_OPTIONS)
  view_path = options.delete(:views) || settings.views || "./views"
  target_extension = File.extname(template_path)[1..-1] || "none" # explicit template extension
  template_path = template_path.chomp(".#{target_extension}")
  # Generate potential template candidates
  templates = Dir[File.join(view_path, template_path) + ".*"].map do |file|
    template_engine = File.extname(file)[1..-1].to_sym # retrieves engine extension
    template_file   = file.sub(view_path, '').chomp(".#{template_engine}").to_sym # retrieves template filename
    [template_file, template_engine] unless IGNORE_FILE_PATTERN.any? { |pattern| template_engine.to_s =~ pattern }
  end
  # Check if we have a simple content type
  simple_content_type = [:html, :plain].include?(content_type)
  # Resolve final template to render
  located_template =
    templates.find { |file, e| file.to_s == "#{template_path}.#{locale}.#{content_type}" } ||
    templates.find { |file, e| file.to_s == "#{template_path}.#{locale}" && simple_content_type } ||
    templates.find { |file, e| File.extname(file.to_s) == ".#{target_extension}" or e.to_s == target_extension.to_s } ||
    templates.find { |file, e| file.to_s == "#{template_path}.#{content_type}" } ||
    templates.find { |file, e| file.to_s == "#{template_path}" && simple_content_type } ||
    (!options[:strict_format] && templates.first) # If not strict, fall back to the first located template
  raise TemplateNotFound, "Template '#{template_path}' not found in '#{view_path}'!"  if !located_template && options[:raise_exceptions]
  settings.cache_template_file!(located_template, rendering_options) unless settings.reload_templates?
  logger.debug :template, began_at, located_template[0] if located_template && settings.logging? && defined?(logger)
  located_template
end