module Middleman::CoreExtensions::Rendering::InstanceMethods

def resolve_template(request_path, options={})

Returns:
  • (Array, Boolean) -

Options Hash: (**options)
  • :try_static (Boolean) --
  • :try_without_underscore (Boolean) --
  • :preferred_engine (Boolean) -- If set, try this engine first, then fall back to any engine.

Parameters:
  • 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, source_dir)
    preferred_engines = if options[:try_static]
      [nil]
    else
      possible_engines = ['*'] # By default, any engine will do
      # If we're specifically looking for a preferred engine
      if options.key?(:preferred_engine)
        extension_class = ::Tilt[options[:preferred_engine]]
        # Get a list of extensions for a preferred engine
        matched_exts = ::Tilt.mappings.select do |_, engines|
          engines.include? extension_class
        end.keys
        # Prefer to look for the matched extensions
        unless matched_exts.empty?
          possible_engines.unshift('{' + matched_exts.join(',') + '}')
        end
      end
      possible_engines
    end
    search_paths = preferred_engines.flat_map do |preferred_engine|
      path_with_ext = on_disk_path.dup
      path_with_ext << ('.' + preferred_engine) unless preferred_engine.nil?
      paths = [path_with_ext]
      if options[:try_without_underscore]
        paths << path_with_ext.sub(relative_path, relative_path.sub(/^_/, '').sub(/\/_/, '/'))
      end
      paths
    end
    found_path = nil
    search_paths.each do |path_with_ext|
      found_path = Dir[path_with_ext].find do |path|
        ::Tilt[path]
      end
      unless found_path
        found_path = path_with_ext if File.exist?(path_with_ext)
      end
      break if found_path
    end
    # If we found one, return it
    if found_path
      found_path
    elsif File.exist?(on_disk_path)
      on_disk_path
    else
      false
    end
  end
end