From beff6805340203434d2b8ca738194b781fd37b83 Mon Sep 17 00:00:00 2001 From: bep Date: Sat, 18 Apr 2015 13:58:35 +0200 Subject: [PATCH] Expand the ACE base template lookup pattern The previous implementation didn't easily support the use case "I want one base template for the single pages, another for the rest". The new lookup order is: 1. /-baseof.ace, e.g. list-baseof.ace 2. /baseof.ace 3. _default/-baseof.ace, e.g. list-baseof.ace. 4. _default/baseof.ace --- tpl/template.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/tpl/template.go b/tpl/template.go index bc3a57a70..c23dad754 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -15,6 +15,7 @@ package tpl import ( "bytes" + "fmt" "github.com/eknkc/amber" bp "github.com/spf13/hugo/bufferpool" "github.com/spf13/hugo/helpers" @@ -279,14 +280,26 @@ func (t *GoHTMLTemplate) loadTemplates(absPath string, prefix string) { return err } if needsBase { - // Look for the base first in the current path, then in _default. - p := filepath.Join(filepath.Dir(path), baseAceFilename) - if ok, err := helpers.Exists(p, hugofs.OsFs); err == nil && ok { - baseTemplatePath = p - } else { - p := filepath.Join(absPath, "_default", baseAceFilename) - if ok, err := helpers.Exists(p, hugofs.OsFs); err == nil && ok { - baseTemplatePath = p + + // Look for base template in the follwing order: + // 1. /-baseof.ace, e.g. list-baseof.ace. + // 2. /baseof.ace + // 3. _default/-baseof.ace, e.g. list-baseof.ace. + // 4. _default/baseof.ace + + currBaseAceFilename := fmt.Sprintf("%s-%s", helpers.Filename(path), baseAceFilename) + templateDir := filepath.Dir(path) + + pathsToCheck := []string{ + filepath.Join(templateDir, currBaseAceFilename), + filepath.Join(templateDir, baseAceFilename), + filepath.Join(absPath, "_default", currBaseAceFilename), + filepath.Join(absPath, "_default", baseAceFilename)} + + for _, pathToCheck := range pathsToCheck { + if ok, err := helpers.Exists(pathToCheck, hugofs.OsFs); err == nil && ok { + baseTemplatePath = pathToCheck + break } } }