From 5de9e86a5529da888907472d7200264596a3cd9b Mon Sep 17 00:00:00 2001 From: Steven Sloan Date: Fri, 13 May 2016 16:01:21 -0400 Subject: [PATCH] allow partial lookups without a current_resource (#1912) current_resource is only needed for relative lookups, so for lookups from source allow them to run without a current resource. --- .../lib/middleman-core/template_context.rb | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/middleman-core/lib/middleman-core/template_context.rb b/middleman-core/lib/middleman-core/template_context.rb index 0842b846..defb063f 100644 --- a/middleman-core/lib/middleman-core/template_context.rb +++ b/middleman-core/lib/middleman-core/template_context.rb @@ -127,30 +127,36 @@ module Middleman # @return [String] Contract String, Maybe[Bool] => Maybe[IsA['Middleman::SourceFile']] def locate_partial(partial_path, try_static=true) - return unless resource = sitemap.find_resource_by_destination_path(current_path) - - # Look for partials relative to the current path - current_dir = resource.file_descriptor[:relative_path].dirname - non_root = partial_path.to_s.sub(/^\//, '') - relative_dir = current_dir + Pathname(non_root) - - non_root_no_underscore = non_root.sub(/^_/, '').sub(/\/_/, '/') - relative_dir_no_underscore = current_dir + Pathname(non_root_no_underscore) - partial_file = nil + lookup_stack = [] + non_root = partial_path.to_s.sub(/^\//, '') + non_root_no_underscore = non_root.sub(/^_/, '').sub(/\/_/, '/') - [ - [relative_dir.to_s, { preferred_engine: resource.file_descriptor[:relative_path].extname[1..-1].to_sym }], - [non_root], - [non_root, { try_static: try_static }], - [relative_dir_no_underscore.to_s, { try_static: try_static }], - [non_root_no_underscore, { try_static: try_static }] - ].each do |args| + if resource = current_resource + current_dir = resource.file_descriptor[:relative_path].dirname + relative_dir = current_dir + Pathname(non_root) + relative_dir_no_underscore = current_dir + Pathname(non_root_no_underscore) + end + + + lookup_stack.push [ relative_dir.to_s, + { preferred_engine: resource.file_descriptor[:relative_path] + .extname[1..-1].to_sym }] if relative_dir + lookup_stack.push [ non_root ] + lookup_stack.push [ non_root, + { try_static: try_static }] + lookup_stack.push [ relative_dir_no_underscore.to_s, + { try_static: try_static }] if relative_dir_no_underscore + lookup_stack.push [ non_root_no_underscore, + { try_static: try_static }] + + + lookup_stack.each do |args| partial_file = ::Middleman::TemplateRenderer.resolve_template(@app, *args) break if partial_file end - partial_file || nil + partial_file end def current_path