From 50bc103e1f9e1f44e861862965ee9a7e34cb9ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 23 Jan 2020 17:34:19 +0100 Subject: [PATCH] Fix baseof with regular define regression Fixes #6790 --- hugolib/template_test.go | 37 ++++++++++++++++++++++++++++ tpl/tplimpl/template.go | 44 +++++++++++++++++++++++----------- tpl/tplimpl/template_errors.go | 4 ++++ 3 files changed, 71 insertions(+), 14 deletions(-) diff --git a/hugolib/template_test.go b/hugolib/template_test.go index e75bda790d6..44566f5a750 100644 --- a/hugolib/template_test.go +++ b/hugolib/template_test.go @@ -361,6 +361,43 @@ Base %d: {{ block "main" . }}FOO{{ end }} } +// https://github.com/gohugoio/hugo/issues/6790 +func TestTemplateNoBasePlease(t *testing.T) { + t.Parallel() + b := newTestSitesBuilder(t).WithSimpleConfigFile() + + b.WithTemplates("_default/list.html", ` + {{ define "main" }} + Bonjour + {{ end }} + + {{ printf "list" }} + + + `) + + b.WithTemplates( + "_default/single.html", ` +{{ printf "single" }} +{{ define "main" }} + Bonjour +{{ end }} + + +`) + + b.WithContent("blog/p1.md", `--- +title: The Page +--- +`) + + b.Build(BuildCfg{}) + + b.AssertFileContent("public/blog/p1/index.html", `single`) + b.AssertFileContent("public/blog/index.html", `list`) + +} + func TestTemplateLookupSite(t *testing.T) { t.Run("basic", func(t *testing.T) { t.Parallel() diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index d0c656a2efb..f089f9ebf6c 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -72,7 +72,9 @@ var ( _ tpl.Info = (*templateState)(nil) ) -var defineRe = regexp.MustCompile(`{{-?\s?define`) +// A template needing a base template is a template with only define sections, +// but we check only for the start. +var defineRe = regexp.MustCompile(`^\s*{{-?\s?define`) func newIdentity(name string) identity.Manager { return identity.NewManager(identity.NewPathIdentity(files.ComponentFolderLayouts, name)) @@ -379,20 +381,19 @@ func (t *templateHandler) findLayout(d output.LayoutDescriptor, f output.Format) } } - if !found { - return nil, false, errors.Errorf("no baseof layout found for %q:", name) - } - templ, err := t.applyBaseTemplate(overlay, base) if err != nil { return nil, false, err } ts := newTemplateState(templ, overlay) - ts.baseInfo = base - // Add the base identity to detect changes - ts.Add(identity.NewPathIdentity(files.ComponentFolderLayouts, base.name)) + if found { + ts.baseInfo = base + + // Add the base identity to detect changes + ts.Add(identity.NewPathIdentity(files.ComponentFolderLayouts, base.name)) + } t.applyTemplateTransformers(t.main, ts) @@ -565,10 +566,18 @@ func (t *templateHandler) addTemplateTo(info templateInfo, to *templateNamespace func (t *templateHandler) applyBaseTemplate(overlay, base templateInfo) (tpl.Template, error) { if overlay.isText { - templ, err := t.main.prototypeTextClone.New(overlay.name).Parse(base.template) - if err != nil { - return nil, base.errWithFileContext("parse failed", err) + var ( + templ = t.main.prototypeTextClone.New(overlay.name) + err error + ) + + if !base.IsZero() { + templ, err = templ.Parse(base.template) + if err != nil { + return nil, base.errWithFileContext("parse failed", err) + } } + templ, err = templ.Parse(overlay.template) if err != nil { return nil, overlay.errWithFileContext("parse failed", err) @@ -576,9 +585,16 @@ func (t *templateHandler) applyBaseTemplate(overlay, base templateInfo) (tpl.Tem return templ, nil } - templ, err := t.main.prototypeHTMLClone.New(overlay.name).Parse(base.template) - if err != nil { - return nil, base.errWithFileContext("parse failed", err) + var ( + templ = t.main.prototypeHTMLClone.New(overlay.name) + err error + ) + + if !base.IsZero() { + templ, err = templ.Parse(base.template) + if err != nil { + return nil, base.errWithFileContext("parse failed", err) + } } templ, err = htmltemplate.Must(templ.Clone()).Parse(overlay.template) diff --git a/tpl/tplimpl/template_errors.go b/tpl/tplimpl/template_errors.go index 48818cb60bf..df80726f5d6 100644 --- a/tpl/tplimpl/template_errors.go +++ b/tpl/tplimpl/template_errors.go @@ -34,6 +34,10 @@ type templateInfo struct { realFilename string } +func (t templateInfo) IsZero() bool { + return t.name == "" +} + func (t templateInfo) resolveType() templateType { return resolveTemplateType(t.name) }