Reimplement and simplify Hugo's template system

See #13541 for details.

Fixes #13545
Fixes #13515
Closes #7964
Closes #13365
Closes #12988
Closes #4891
This commit is contained in:
Bjørn Erik Pedersen
2025-04-06 19:55:35 +02:00
parent 812ea0b325
commit 83cfdd78ca
138 changed files with 5342 additions and 4396 deletions

View File

@@ -26,6 +26,8 @@ import (
"github.com/gohugoio/hugo/hugofs"
)
// TODO(bep) keep this until we release v0.146.0 as a security against breaking changes, but it's rather messy and mostly duplicate of
// tests in the tplimpl package, so eventually just remove it.
func TestTemplateLookupOrder(t *testing.T) {
var (
fs *hugofs.Fs
@@ -185,6 +187,9 @@ func TestTemplateLookupOrder(t *testing.T) {
} {
this := this
if this.name != "Variant 1" {
continue
}
t.Run(this.name, func(t *testing.T) {
// TODO(bep) there are some function vars need to pull down here to enable => t.Parallel()
cfg, fs = newTestCfg()
@@ -200,7 +205,7 @@ Some content
}
buildSingleSite(t, deps.DepsCfg{Fs: fs, Configs: configs}, BuildCfg{})
// helpers.PrintFs(s.BaseFs.Layouts.Fs, "", os.Stdout)
// s.TemplateStore.PrintDebug("", 0, os.Stdout)
this.assert(t)
})
@@ -270,11 +275,11 @@ func TestTemplateNoBasePlease(t *testing.T) {
b := newTestSitesBuilder(t).WithSimpleConfigFile()
b.WithTemplates("_default/list.html", `
{{ define "main" }}
Bonjour
{{ end }}
{{ define "main" }}
Bonjour
{{ end }}
{{ printf "list" }}
{{ printf "list" }}
`)
@@ -344,33 +349,36 @@ title: %s
b.AssertFileContent("public/p1/index.html", `Single: P1`)
})
t.Run("baseof", func(t *testing.T) {
t.Parallel()
b := newTestSitesBuilder(t).WithDefaultMultiSiteConfig()
{
}
}
b.WithTemplatesAdded(
"index.html", `{{ define "main" }}Main Home En{{ end }}`,
"index.fr.html", `{{ define "main" }}Main Home Fr{{ end }}`,
"baseof.html", `Baseof en: {{ block "main" . }}main block{{ end }}`,
"baseof.fr.html", `Baseof fr: {{ block "main" . }}main block{{ end }}`,
"mysection/baseof.html", `Baseof mysection: {{ block "main" . }}mysection block{{ end }}`,
"_default/single.html", `{{ define "main" }}Main Default Single{{ end }}`,
"_default/list.html", `{{ define "main" }}Main Default List{{ end }}`,
)
func TestTemplateLookupSitBaseOf(t *testing.T) {
t.Parallel()
b := newTestSitesBuilder(t).WithDefaultMultiSiteConfig()
b.WithContent("mysection/p1.md", `---
b.WithTemplatesAdded(
"index.html", `{{ define "main" }}Main Home En{{ end }}`,
"index.fr.html", `{{ define "main" }}Main Home Fr{{ end }}`,
"baseof.html", `Baseof en: {{ block "main" . }}main block{{ end }}`,
"baseof.fr.html", `Baseof fr: {{ block "main" . }}main block{{ end }}`,
"mysection/baseof.html", `Baseof mysection: {{ block "main" . }}mysection block{{ end }}`,
"_default/single.html", `{{ define "main" }}Main Default Single{{ end }}`,
"_default/list.html", `{{ define "main" }}Main Default List{{ end }}`,
)
b.WithContent("mysection/p1.md", `---
title: My Page
---
`)
b.CreateSites().Build(BuildCfg{})
b.CreateSites().Build(BuildCfg{})
b.AssertFileContent("public/en/index.html", `Baseof en: Main Home En`)
b.AssertFileContent("public/fr/index.html", `Baseof fr: Main Home Fr`)
b.AssertFileContent("public/en/mysection/index.html", `Baseof mysection: Main Default List`)
b.AssertFileContent("public/en/mysection/p1/index.html", `Baseof mysection: Main Default Single`)
})
b.AssertFileContent("public/en/index.html", `Baseof en: Main Home En`)
b.AssertFileContent("public/fr/index.html", `Baseof fr: Main Home Fr`)
b.AssertFileContent("public/en/mysection/index.html", `Baseof mysection: Main Default List`)
b.AssertFileContent("public/en/mysection/p1/index.html", `Baseof mysection: Main Default Single`)
}
func TestTemplateFuncs(t *testing.T) {
@@ -707,6 +715,7 @@ a: {{ $a }}
b.AssertFileContent("public/index.html", `a: [a b c]`)
}
// Legacy behavior for internal templates.
func TestOverrideInternalTemplate(t *testing.T) {
files := `
-- hugo.toml --