// Copyright 2025 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package hugolib import ( "fmt" "strings" "testing" qt "github.com/frankban/quicktest" ) // https://github.com/gohugoio/hugo/issues/4895 func TestTemplateBOM(t *testing.T) { t.Parallel() bom := "\ufeff" files := ` -- hugo.toml -- baseURL = "http://example.com/" -- layouts/baseof.html -- ` + bom + ` Base: {{ block "main" . }}base main{{ end }} -- layouts/single.html -- ` + bom + `{{ define "main" }}Hi!?{{ end }} -- content/page.md -- --- title: "Page" --- Page Content ` b := Test(t, files) b.AssertFileContent("public/page/index.html", "Base: Hi!?") } func TestTemplateManyBaseTemplates(t *testing.T) { t.Parallel() numPages := 100 var b strings.Builder b.WriteString("-- hugo.toml --\n") b.WriteString("baseURL = \"http://example.com/\"\n") for i := range numPages { id := i + 1 b.WriteString(fmt.Sprintf("-- content/page%d.md --\n", id)) b.WriteString(fmt.Sprintf(`--- title: "Page %d" layout: "layout%d" --- Content. `, id, id)) b.WriteString(fmt.Sprintf("-- layouts/layout%d.html --\n", id)) b.WriteString(fmt.Sprintf(` {{ define "main" }}%d{{ end }} `, id)) b.WriteString(fmt.Sprintf("-- layouts/layout%d-baseof.html --\n", id)) b.WriteString(fmt.Sprintf(` Base %d: {{ block "main" . }}FOO{{ end }} `, id)) } files := b.String() builder := Test(t, files) for i := range numPages { id := i + 1 builder.AssertFileContent(fmt.Sprintf("public/page%d/index.html", id), fmt.Sprintf(`Base %d: %d`, id, id)) } } // https://github.com/gohugoio/hugo/issues/6790 func TestTemplateNoBasePlease(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- baseURL = "http://example.com/" -- layouts/list.html -- {{ define "main" }} Bonjour {{ end }} {{ printf "list" }} -- layouts/single.html -- {{ printf "single" }} {{ define "main" }} Bonjour {{ end }} -- content/blog/p1.md -- --- title: The Page --- ` b := Test(t, files) b.AssertFileContent("public/blog/p1/index.html", `single`) b.AssertFileContent("public/blog/index.html", `list`) } // https://github.com/gohugoio/hugo/issues/6816 func TestTemplateBaseWithComment(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- baseURL = "http://example.com/" -- layouts/baseof.html -- Base: {{ block "main" . }}{{ end }} -- layouts/home.html -- {{/* A comment */}} {{ define "main" }} Bonjour {{ end }} ` b := Test(t, files) b.AssertFileContent("public/index.html", `Base: Bonjour`) } func TestTemplateLookupSite(t *testing.T) { t.Run("basic", func(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- baseURL = "http://example.com/" -- layouts/single.html -- Single: {{ .Title }} -- layouts/list.html -- List: {{ .Title }} -- content/_index.md -- --- title: Home Sweet Home --- -- content/p1.md -- --- title: P1 --- ` b := Test(t, files) b.AssertFileContent("public/index.html", `List: Home Sweet Home`) b.AssertFileContent("public/p1/index.html", `Single: P1`) }) } func TestTemplateLookupSitBaseOf(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- baseURL = "http://example.com/blog" disablePathToLower = true defaultContentLanguage = "en" defaultContentLanguageInSubdir = true [languages] [languages.en] weight = 10 [languages.fr] weight = 20 -- layouts/home.html -- {{ define "main" }}Main Home En{{ end }} -- layouts/home.fr.html -- {{ define "main" }}Main Home Fr{{ end }} -- layouts/baseof.html -- Baseof en: {{ block "main" . }}main block{{ end }} -- layouts/baseof.fr.html -- Baseof fr: {{ block "main" . }}main block{{ end }} -- layouts/mysection/baseof.html -- Baseof mysection: {{ block "main" . }}mysection block{{ end }} -- layouts/single.html -- {{ define "main" }}Main Default Single{{ end }} -- layouts/list.html -- {{ define "main" }}Main Default List{{ end }} -- content/mysection/p1.md -- --- title: My Page --- ` b := Test(t, files) 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) { t.Parallel() files := ` -- hugo.toml -- baseURL = "http://example.com/blog" disablePathToLower = true defaultContentLanguage = "en" defaultContentLanguageInSubdir = true [languages] [languages.en] weight = 10 [languages.fr] weight = 20 -- layouts/home.html -- Site: {{ site.Language.Lang }} / {{ .Site.Language.Lang }} / {{ site.BaseURL }} Sites: {{ hugo.Sites.Default.Home.Language.Lang }} Hugo: {{ hugo.Generator }} -- layouts/home.fr.html -- Site: {{ site.Language.Lang }} / {{ .Site.Language.Lang }} / {{ site.BaseURL }} Sites: {{ hugo.Sites.Default.Home.Language.Lang }} Hugo: {{ hugo.Generator }} ` b := Test(t, files) b.AssertFileContent("public/en/index.html", "Site: en / en / http://example.com/blog", "Sites: en", "Hugo: {"@type":"WebPage","headline":"{{$title}}"} {{/* Action/commands newlines, from Go 1.16, see https://github.com/golang/go/issues/29770 */}} {{ $norway := dict "country" "Norway" "population" "5 millions" "language" "Norwegian" "language_code" "nb" "weather" "freezing cold" "capitol" "Oslo" "largest_city" "Oslo" "currency" "Norwegian krone" "dialing_code" "+47" }} Population in Norway is {{ $norway.population | lower | upper }} ` b := Test(t, files) b.AssertFileContent("public/index.html", ` Population in Norway is 5 MILLIONS `) } func TestPartialInline(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- baseURL = "http://example.com/" -- content/p1.md -- -- layouts/home.html -- {{ $p1 := partial "p1" . }} {{ $p2 := partial "p2" . }} P1: {{ $p1 }} P2: {{ $p2 }} {{ define "partials/p1" }}Inline: p1{{ end }} {{ define "partials/p2" }} {{ $value := 32 }} {{ return $value }} {{ end }} ` b := Test(t, files) b.AssertFileContent("public/index.html", ` P1: Inline: p1 P2: 32`, ) } func TestPartialInlineBase(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- baseURL = "http://example.com/" -- content/p1.md -- -- layouts/baseof.html -- {{ $p3 := partial "p3" . }}P3: {{ $p3 }} {{ block "main" . }}{{ end }}{{ define "partials/p3" }}Inline: p3{{ end }} -- layouts/home.html -- {{ define "main" }} {{ $p1 := partial "p1" . }} {{ $p2 := partial "p2" . }} P1: {{ $p1 }} P2: {{ $p2 }} {{ end }} {{ define "partials/p1" }}Inline: p1{{ end }} {{ define "partials/p2" }} {{ $value := 32 }} {{ return $value }} {{ end }} ` b := Test(t, files) b.AssertFileContent("public/index.html", ` P1: Inline: p1 P2: 32 P3: Inline: p3 `, ) } // https://github.com/gohugoio/hugo/issues/7478 func TestBaseWithAndWithoutDefine(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- baseURL = "http://example.com/" -- content/p1.md -- --- title: P --- Content -- layouts/baseof.html -- ::Header Start:{{ block "header" . }}{{ end }}:Header End: ::{{ block "main" . }}Main{{ end }}:: -- layouts/home.html -- {{ define "header" }} Home Header {{ end }} {{ define "main" }} This is home main {{ end }} -- layouts/single.html -- {{ define "main" }} This is single main {{ end }} ` b := Test(t, files) b.AssertFileContent("public/index.html", ` Home Header This is home main `, ) b.AssertFileContent("public/p1/index.html", ` ::Header Start::Header End: This is single main `, ) } // Issue 9393. func TestApplyWithNamespace(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- baseURL = "http://example.com/" -- content/p1.md -- -- layouts/home.html -- {{ $b := slice " a " " b " " c" }} {{ $a := apply $b "strings.Trim" "." " " }} a: {{ $a }} ` b := Test(t, files) b.AssertFileContent("public/index.html", `a: [a b c]`) } // Legacy behavior for internal templates. func TestOverrideInternalTemplate(t *testing.T) { files := ` -- hugo.toml -- baseURL = "https://example.org" -- layouts/home.html -- {{ template "_internal/google_analytics_async.html" . }} -- layouts/_internal/google_analytics_async.html -- Overridden. ` b := Test(t, files) b.AssertFileContent("public/index.html", "Overridden.") } // Issue 13877 func TestTemplateSelectionFirstMediaTypeSuffix(t *testing.T) { t.Parallel() files := ` -- hugo.toml -- disableKinds = ['home','rss','section','sitemap','taxonomy','term'] [mediaTypes.'text/html'] suffixes = ['b','a','d','c'] [outputFormats.html] mediaType = 'text/html' -- content/p1.md -- --- title: p1 --- -- layouts/page.html.a -- page.html.a -- layouts/page.html.b -- page.html.b -- layouts/page.html.c -- page.html.c -- layouts/page.html.d -- page.html.d ` b := Test(t, files) b.AssertFileContent("public/p1/index.b", "page.html.b") }