module Middleman::CoreExtensions::Rendering::InstanceMethods

def resolve_template(request_path, options={})

Returns:
  • (Array, Boolean) -

Parameters:
  • options (Hash) --
  • request_path (String) --
def resolve_template(request_path, options={})
  # Find the path by searching or using the cache
  request_path = request_path.to_s
  cache.fetch(:resolve_template, request_path, options) do
    relative_path = Util.strip_leading_slash(request_path)
    on_disk_path  = File.expand_path(relative_path, self.source_dir)
    # By default, any engine will do
    preferred_engine = "*"
    # Unless we're specifically looking for a preferred engine
    if options.has_key?(:preferred_engine)
      extension_class = ::Tilt[options[:preferred_engine]]
      matched_exts = []
      # Get a list of extensions for a preferred engine
      # TODO: Cache this
      ::Tilt.mappings.each do |ext, engines|
        next unless engines.include? extension_class
        matched_exts << ext
      end
      # Change the glob to only look for the matched extensions
      if matched_exts.length > 0
        preferred_engine = "{" + matched_exts.join(",") + "}"
      else
        return false
      end
    end
    # Look for files that match
    path_with_ext = on_disk_path + "." + preferred_engine
    found_path = Dir[path_with_ext].find do |path|
      ::Tilt[path]
    end
    if !found_path && options[:try_without_underscore] &&
      path_no_underscore = path_with_ext.
        sub(relative_path, relative_path.sub(/^_/, "").
        sub(/\/_/, "/"))
      found_path = Dir[path_no_underscore].find do |path|
        ::Tilt[path]
      end
    end
    # If we found one, return it and the found engine
    if found_path || (File.exists?(on_disk_path) && !File.directory?(on_disk_path))
      engine = found_path ? File.extname(found_path)[1..-1].to_sym : nil
      [ found_path || on_disk_path, engine ]
    else
      false
    end
  end
end