| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hugolib |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
qt "github.com/frankban/quicktest" |
| 22 |
) |
| 23 |
|
| 24 |
// https://github.com/gohugoio/hugo/issues/4895 |
| 25 |
func TestTemplateBOM(t *testing.T) { |
| 26 |
t.Parallel() |
| 27 |
|
| 28 |
bom := "\ufeff" |
| 29 |
|
| 30 |
files := ` |
| 31 |
-- hugo.toml -- |
| 32 |
baseURL = "http://example.com/" |
| 33 |
-- layouts/baseof.html -- |
| 34 |
` + bom + ` |
| 35 |
Base: {{ block "main" . }}base main{{ end }} |
| 36 |
-- layouts/single.html -- |
| 37 |
` + bom + `{{ define "main" }}Hi!?{{ end }} |
| 38 |
-- content/page.md -- |
| 39 |
--- |
| 40 |
title: "Page" |
| 41 |
--- |
| 42 |
|
| 43 |
Page Content |
| 44 |
` |
| 45 |
b := Test(t, files) |
| 46 |
|
| 47 |
b.AssertFileContent("public/page/index.html", "Base: Hi!?") |
| 48 |
} |
| 49 |
|
| 50 |
func TestTemplateManyBaseTemplates(t *testing.T) { |
| 51 |
t.Parallel() |
| 52 |
|
| 53 |
numPages := 100 |
| 54 |
|
| 55 |
var b strings.Builder |
| 56 |
b.WriteString("-- hugo.toml --\n") |
| 57 |
b.WriteString("baseURL = \"http://example.com/\"\n") |
| 58 |
|
| 59 |
for i := range numPages { |
| 60 |
id := i + 1 |
| 61 |
b.WriteString(fmt.Sprintf("-- content/page%d.md --\n", id)) |
| 62 |
b.WriteString(fmt.Sprintf(`--- |
| 63 |
title: "Page %d" |
| 64 |
layout: "layout%d" |
| 65 |
--- |
| 66 |
|
| 67 |
Content. |
| 68 |
`, id, id)) |
| 69 |
|
| 70 |
b.WriteString(fmt.Sprintf("-- layouts/layout%d.html --\n", id)) |
| 71 |
b.WriteString(fmt.Sprintf(` |
| 72 |
{{ define "main" }}%d{{ end }} |
| 73 |
`, id)) |
| 74 |
|
| 75 |
b.WriteString(fmt.Sprintf("-- layouts/layout%d-baseof.html --\n", id)) |
| 76 |
b.WriteString(fmt.Sprintf(` |
| 77 |
Base %d: {{ block "main" . }}FOO{{ end }} |
| 78 |
`, id)) |
| 79 |
} |
| 80 |
|
| 81 |
files := b.String() |
| 82 |
|
| 83 |
builder := Test(t, files) |
| 84 |
|
| 85 |
for i := range numPages { |
| 86 |
id := i + 1 |
| 87 |
builder.AssertFileContent(fmt.Sprintf("public/page%d/index.html", id), fmt.Sprintf(`Base %d: %d`, id, id)) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// https://github.com/gohugoio/hugo/issues/6790 |
| 92 |
func TestTemplateNoBasePlease(t *testing.T) { |
| 93 |
t.Parallel() |
| 94 |
|
| 95 |
files := ` |
| 96 |
-- hugo.toml -- |
| 97 |
baseURL = "http://example.com/" |
| 98 |
-- layouts/list.html -- |
| 99 |
{{ define "main" }} |
| 100 |
Bonjour |
| 101 |
{{ end }} |
| 102 |
|
| 103 |
{{ printf "list" }} |
| 104 |
-- layouts/single.html -- |
| 105 |
{{ printf "single" }} |
| 106 |
{{ define "main" }} |
| 107 |
Bonjour |
| 108 |
{{ end }} |
| 109 |
-- content/blog/p1.md -- |
| 110 |
--- |
| 111 |
title: The Page |
| 112 |
--- |
| 113 |
` |
| 114 |
b := Test(t, files) |
| 115 |
|
| 116 |
b.AssertFileContent("public/blog/p1/index.html", `single`) |
| 117 |
b.AssertFileContent("public/blog/index.html", `list`) |
| 118 |
} |
| 119 |
|
| 120 |
// https://github.com/gohugoio/hugo/issues/6816 |
| 121 |
func TestTemplateBaseWithComment(t *testing.T) { |
| 122 |
t.Parallel() |
| 123 |
|
| 124 |
files := ` |
| 125 |
-- hugo.toml -- |
| 126 |
baseURL = "http://example.com/" |
| 127 |
-- layouts/baseof.html -- |
| 128 |
Base: {{ block "main" . }}{{ end }} |
| 129 |
-- layouts/home.html -- |
| 130 |
{{/* A comment */}} |
| 131 |
{{ define "main" }} |
| 132 |
Bonjour |
| 133 |
{{ end }} |
| 134 |
` |
| 135 |
b := Test(t, files) |
| 136 |
|
| 137 |
b.AssertFileContent("public/index.html", `Base: |
| 138 |
Bonjour`) |
| 139 |
} |
| 140 |
|
| 141 |
func TestTemplateLookupSite(t *testing.T) { |
| 142 |
t.Run("basic", func(t *testing.T) { |
| 143 |
t.Parallel() |
| 144 |
|
| 145 |
files := ` |
| 146 |
-- hugo.toml -- |
| 147 |
baseURL = "http://example.com/" |
| 148 |
-- layouts/single.html -- |
| 149 |
Single: {{ .Title }} |
| 150 |
-- layouts/list.html -- |
| 151 |
List: {{ .Title }} |
| 152 |
-- content/_index.md -- |
| 153 |
--- |
| 154 |
title: Home Sweet Home |
| 155 |
--- |
| 156 |
-- content/p1.md -- |
| 157 |
--- |
| 158 |
title: P1 |
| 159 |
--- |
| 160 |
` |
| 161 |
b := Test(t, files) |
| 162 |
|
| 163 |
b.AssertFileContent("public/index.html", `List: Home Sweet Home`) |
| 164 |
b.AssertFileContent("public/p1/index.html", `Single: P1`) |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
func TestTemplateLookupSitBaseOf(t *testing.T) { |
| 169 |
t.Parallel() |
| 170 |
|
| 171 |
files := ` |
| 172 |
-- hugo.toml -- |
| 173 |
baseURL = "http://example.com/blog" |
| 174 |
disablePathToLower = true |
| 175 |
defaultContentLanguage = "en" |
| 176 |
defaultContentLanguageInSubdir = true |
| 177 |
|
| 178 |
[languages] |
| 179 |
[languages.en] |
| 180 |
weight = 10 |
| 181 |
[languages.fr] |
| 182 |
weight = 20 |
| 183 |
|
| 184 |
-- layouts/home.html -- |
| 185 |
{{ define "main" }}Main Home En{{ end }} |
| 186 |
-- layouts/home.fr.html -- |
| 187 |
{{ define "main" }}Main Home Fr{{ end }} |
| 188 |
-- layouts/baseof.html -- |
| 189 |
Baseof en: {{ block "main" . }}main block{{ end }} |
| 190 |
-- layouts/baseof.fr.html -- |
| 191 |
Baseof fr: {{ block "main" . }}main block{{ end }} |
| 192 |
-- layouts/mysection/baseof.html -- |
| 193 |
Baseof mysection: {{ block "main" . }}mysection block{{ end }} |
| 194 |
-- layouts/single.html -- |
| 195 |
{{ define "main" }}Main Default Single{{ end }} |
| 196 |
-- layouts/list.html -- |
| 197 |
{{ define "main" }}Main Default List{{ end }} |
| 198 |
-- content/mysection/p1.md -- |
| 199 |
--- |
| 200 |
title: My Page |
| 201 |
--- |
| 202 |
` |
| 203 |
b := Test(t, files) |
| 204 |
|
| 205 |
b.AssertFileContent("public/en/index.html", `Baseof en: Main Home En`) |
| 206 |
b.AssertFileContent("public/fr/index.html", `Baseof fr: Main Home Fr`) |
| 207 |
b.AssertFileContent("public/en/mysection/index.html", `Baseof mysection: Main Default List`) |
| 208 |
b.AssertFileContent("public/en/mysection/p1/index.html", `Baseof mysection: Main Default Single`) |
| 209 |
} |
| 210 |
|
| 211 |
func TestTemplateFuncs(t *testing.T) { |
| 212 |
t.Parallel() |
| 213 |
|
| 214 |
files := ` |
| 215 |
-- hugo.toml -- |
| 216 |
baseURL = "http://example.com/blog" |
| 217 |
disablePathToLower = true |
| 218 |
defaultContentLanguage = "en" |
| 219 |
defaultContentLanguageInSubdir = true |
| 220 |
|
| 221 |
[languages] |
| 222 |
[languages.en] |
| 223 |
weight = 10 |
| 224 |
[languages.fr] |
| 225 |
weight = 20 |
| 226 |
|
| 227 |
-- layouts/home.html -- |
| 228 |
Site: {{ site.Language.Lang }} / {{ .Site.Language.Lang }} / {{ site.BaseURL }} |
| 229 |
Sites: {{ hugo.Sites.Default.Home.Language.Lang }} |
| 230 |
Hugo: {{ hugo.Generator }} |
| 231 |
-- layouts/home.fr.html -- |
| 232 |
Site: {{ site.Language.Lang }} / {{ .Site.Language.Lang }} / {{ site.BaseURL }} |
| 233 |
Sites: {{ hugo.Sites.Default.Home.Language.Lang }} |
| 234 |
Hugo: {{ hugo.Generator }} |
| 235 |
` |
| 236 |
b := Test(t, files) |
| 237 |
|
| 238 |
b.AssertFileContent("public/en/index.html", |
| 239 |
"Site: en / en / http://example.com/blog", |
| 240 |
"Sites: en", |
| 241 |
"Hugo: <meta name=\"generator\" content=\"Hugo") |
| 242 |
b.AssertFileContent("public/fr/index.html", |
| 243 |
"Site: fr / fr / http://example.com/blog", |
| 244 |
"Sites: en", |
| 245 |
"Hugo: <meta name=\"generator\" content=\"Hugo", |
| 246 |
) |
| 247 |
} |
| 248 |
|
| 249 |
func TestPartialWithReturn(t *testing.T) { |
| 250 |
t.Parallel() |
| 251 |
|
| 252 |
files := ` |
| 253 |
-- hugo.toml -- |
| 254 |
baseURL = "http://example.com/" |
| 255 |
-- layouts/_partials/add42.tpl -- |
| 256 |
{{ $v := add . 42 }} |
| 257 |
{{ return $v }} |
| 258 |
-- layouts/_partials/dollarContext.tpl -- |
| 259 |
{{ $v := add $ 42 }} |
| 260 |
{{ return $v }} |
| 261 |
-- layouts/_partials/dict.tpl -- |
| 262 |
{{ $v := add $.adder 42 }} |
| 263 |
{{ return $v }} |
| 264 |
-- layouts/_partials/complex.tpl -- |
| 265 |
{{ return add . 42 }} |
| 266 |
-- layouts/_partials/hello.tpl -- |
| 267 |
{{ $v := printf "hello %s" . }} |
| 268 |
{{ return $v }} |
| 269 |
-- layouts/home.html -- |
| 270 |
Test Partials With Return Values: |
| 271 |
|
| 272 |
add42: 50: {{ partial "add42.tpl" 8 }} |
| 273 |
hello world: {{ partial "hello.tpl" "world" }} |
| 274 |
dollarContext: 60: {{ partial "dollarContext.tpl" 18 }} |
| 275 |
adder: 70: {{ partial "dict.tpl" (dict "adder" 28) }} |
| 276 |
complex: 80: {{ partial "complex.tpl" 38 }} |
| 277 |
` |
| 278 |
b := Test(t, files) |
| 279 |
|
| 280 |
b.AssertFileContent("public/index.html", ` |
| 281 |
add42: 50: 50 |
| 282 |
hello world: hello world |
| 283 |
dollarContext: 60: 60 |
| 284 |
adder: 70: 70 |
| 285 |
complex: 80: 80 |
| 286 |
`) |
| 287 |
} |
| 288 |
|
| 289 |
// Issue 7528 |
| 290 |
func TestPartialWithZeroedArgs(t *testing.T) { |
| 291 |
t.Parallel() |
| 292 |
|
| 293 |
files := ` |
| 294 |
-- hugo.toml -- |
| 295 |
baseURL = "http://example.com/" |
| 296 |
-- layouts/home.html -- |
| 297 |
X{{ partial "retval" dict }}X |
| 298 |
X{{ partial "retval" slice }}X |
| 299 |
X{{ partial "retval" "" }}X |
| 300 |
X{{ partial "retval" false }}X |
| 301 |
X{{ partial "retval" 0 }}X |
| 302 |
{{ define "partials/retval" }} |
| 303 |
{{ return 123 }} |
| 304 |
{{ end }} |
| 305 |
-- content/p.md -- |
| 306 |
` |
| 307 |
b := Test(t, files) |
| 308 |
|
| 309 |
b.AssertFileContent("public/index.html", |
| 310 |
` |
| 311 |
X123X |
| 312 |
X123X |
| 313 |
X123X |
| 314 |
X123X |
| 315 |
X123X |
| 316 |
`) |
| 317 |
} |
| 318 |
|
| 319 |
// See issue 15212. |
| 320 |
func TestTemplateReturnEarly(t *testing.T) { |
| 321 |
t.Parallel() |
| 322 |
|
| 323 |
files := ` |
| 324 |
-- hugo.toml -- |
| 325 |
baseURL = "http://example.com/" |
| 326 |
-- layouts/home.html -- |
| 327 |
home-start|{{ if true }}{{ return }}{{ end }}home-end |
| 328 |
` |
| 329 |
b := Test(t, files) |
| 330 |
|
| 331 |
b.AssertFileContent("public/index.html", "home-start|", "! home-end") |
| 332 |
} |
| 333 |
|
| 334 |
// See issue 15212. |
| 335 |
func TestTemplateReturnEarlyFromBlock(t *testing.T) { |
| 336 |
t.Parallel() |
| 337 |
|
| 338 |
files := ` |
| 339 |
-- hugo.toml -- |
| 340 |
baseURL = "http://example.com/" |
| 341 |
-- layouts/baseof.html -- |
| 342 |
header|{{ block "main" . }}{{ end }}|footer |
| 343 |
-- layouts/home.html -- |
| 344 |
{{ define "main" }}main-start|{{ return }}main-end{{ end }} |
| 345 |
` |
| 346 |
b := Test(t, files) |
| 347 |
|
| 348 |
b.AssertFileContent("public/index.html", "header|main-start||footer", "! main-end") |
| 349 |
} |
| 350 |
|
| 351 |
// See issue 15212. |
| 352 |
func TestTemplateReturnEarlyFromTemplateInclude(t *testing.T) { |
| 353 |
t.Parallel() |
| 354 |
|
| 355 |
files := ` |
| 356 |
-- hugo.toml -- |
| 357 |
baseURL = "http://example.com/" |
| 358 |
-- layouts/home.html -- |
| 359 |
{{ template "foo" . }}|after |
| 360 |
{{ define "foo" }}foo-start|{{ return }}foo-end{{ end }} |
| 361 |
` |
| 362 |
b := Test(t, files) |
| 363 |
|
| 364 |
b.AssertFileContent("public/index.html", "foo-start||after", "! foo-end") |
| 365 |
} |
| 366 |
|
| 367 |
// See issue 15212. |
| 368 |
func TestTemplateReturnWithValueOutsidePartialFails(t *testing.T) { |
| 369 |
t.Parallel() |
| 370 |
|
| 371 |
files := ` |
| 372 |
-- hugo.toml -- |
| 373 |
baseURL = "http://example.com/" |
| 374 |
-- layouts/home.html -- |
| 375 |
{{ return 32 }} |
| 376 |
` |
| 377 |
b, err := TestE(t, files) |
| 378 |
|
| 379 |
b.Assert(err, qt.IsNotNil) |
| 380 |
b.Assert(err.Error(), qt.Contains, "return with a value is only supported in partials") |
| 381 |
} |
| 382 |
|
| 383 |
func TestPartialCached(t *testing.T) { |
| 384 |
t.Parallel() |
| 385 |
|
| 386 |
files := ` |
| 387 |
-- hugo.toml -- |
| 388 |
baseURL = "http://example.com/" |
| 389 |
-- layouts/home.html -- |
| 390 |
{{ $key1 := (dict "a" "av" ) }} |
| 391 |
{{ $key2 := (dict "a" "av2" ) }} |
| 392 |
Partial cached1: {{ partialCached "p1" "input1" $key1 }} |
| 393 |
Partial cached2: {{ partialCached "p1" "input2" $key1 }} |
| 394 |
Partial cached3: {{ partialCached "p1" "input3" $key2 }} |
| 395 |
-- layouts/_partials/p1.html -- |
| 396 |
partial: {{ . }} |
| 397 |
` |
| 398 |
b := Test(t, files) |
| 399 |
|
| 400 |
b.AssertFileContent("public/index.html", ` |
| 401 |
Partial cached1: partial: input1 |
| 402 |
Partial cached2: partial: input1 |
| 403 |
Partial cached3: partial: input3 |
| 404 |
`) |
| 405 |
} |
| 406 |
|
| 407 |
// https://github.com/gohugoio/hugo/issues/6615 |
| 408 |
func TestTemplateTruth(t *testing.T) { |
| 409 |
t.Parallel() |
| 410 |
|
| 411 |
files := ` |
| 412 |
-- hugo.toml -- |
| 413 |
baseURL = "http://example.com/" |
| 414 |
-- layouts/home.html -- |
| 415 |
{{ $p := index site.RegularPages 0 }} |
| 416 |
{{ $zero := $p.ExpiryDate }} |
| 417 |
{{ $notZero := time.Now }} |
| 418 |
|
| 419 |
if: Zero: {{ if $zero }}FAIL{{ else }}OK{{ end }} |
| 420 |
if: Not Zero: {{ if $notZero }}OK{{ else }}Fail{{ end }} |
| 421 |
not: Zero: {{ if not $zero }}OK{{ else }}FAIL{{ end }} |
| 422 |
not: Not Zero: {{ if not $notZero }}FAIL{{ else }}OK{{ end }} |
| 423 |
|
| 424 |
with: Zero {{ with $zero }}FAIL{{ else }}OK{{ end }} |
| 425 |
-- content/p1.md -- |
| 426 |
--- |
| 427 |
title: p1 |
| 428 |
--- |
| 429 |
` |
| 430 |
b := Test(t, files) |
| 431 |
|
| 432 |
b.AssertFileContent("public/index.html", ` |
| 433 |
if: Zero: OK |
| 434 |
if: Not Zero: OK |
| 435 |
not: Zero: OK |
| 436 |
not: Not Zero: OK |
| 437 |
with: Zero OK |
| 438 |
`) |
| 439 |
} |
| 440 |
|
| 441 |
func TestTemplateGoIssues(t *testing.T) { |
| 442 |
t.Parallel() |
| 443 |
|
| 444 |
files := ` |
| 445 |
-- hugo.toml -- |
| 446 |
baseURL = "http://example.com/" |
| 447 |
-- layouts/home.html -- |
| 448 |
{{ $title := "a & b" }} |
| 449 |
<script type="application/ld+json">{"@type":"WebPage","headline":"{{$title}}"}</script> |
| 450 |
|
| 451 |
{{/* Action/commands newlines, from Go 1.16, see https://github.com/golang/go/issues/29770 */}} |
| 452 |
{{ $norway := dict |
| 453 |
"country" "Norway" |
| 454 |
"population" "5 millions" |
| 455 |
"language" "Norwegian" |
| 456 |
"language_code" "nb" |
| 457 |
"weather" "freezing cold" |
| 458 |
"capitol" "Oslo" |
| 459 |
"largest_city" "Oslo" |
| 460 |
"currency" "Norwegian krone" |
| 461 |
"dialing_code" "+47" |
| 462 |
}} |
| 463 |
|
| 464 |
Population in Norway is {{ |
| 465 |
$norway.population |
| 466 |
| lower |
| 467 |
| upper |
| 468 |
}} |
| 469 |
` |
| 470 |
b := Test(t, files) |
| 471 |
|
| 472 |
b.AssertFileContent("public/index.html", ` |
| 473 |
<script type="application/ld+json">{"@type":"WebPage","headline":"a \u0026 b"}</script> |
| 474 |
Population in Norway is 5 MILLIONS |
| 475 |
|
| 476 |
`) |
| 477 |
} |
| 478 |
|
| 479 |
func TestPartialInline(t *testing.T) { |
| 480 |
t.Parallel() |
| 481 |
|
| 482 |
files := ` |
| 483 |
-- hugo.toml -- |
| 484 |
baseURL = "http://example.com/" |
| 485 |
-- content/p1.md -- |
| 486 |
-- layouts/home.html -- |
| 487 |
{{ $p1 := partial "p1" . }} |
| 488 |
{{ $p2 := partial "p2" . }} |
| 489 |
|
| 490 |
P1: {{ $p1 }} |
| 491 |
P2: {{ $p2 }} |
| 492 |
|
| 493 |
{{ define "partials/p1" }}Inline: p1{{ end }} |
| 494 |
|
| 495 |
{{ define "partials/p2" }} |
| 496 |
{{ $value := 32 }} |
| 497 |
{{ return $value }} |
| 498 |
{{ end }} |
| 499 |
` |
| 500 |
b := Test(t, files) |
| 501 |
|
| 502 |
b.AssertFileContent("public/index.html", |
| 503 |
` |
| 504 |
P1: Inline: p1 |
| 505 |
P2: 32`, |
| 506 |
) |
| 507 |
} |
| 508 |
|
| 509 |
func TestPartialInlineBase(t *testing.T) { |
| 510 |
t.Parallel() |
| 511 |
|
| 512 |
files := ` |
| 513 |
-- hugo.toml -- |
| 514 |
baseURL = "http://example.com/" |
| 515 |
-- content/p1.md -- |
| 516 |
-- layouts/baseof.html -- |
| 517 |
{{ $p3 := partial "p3" . }}P3: {{ $p3 }} |
| 518 |
{{ block "main" . }}{{ end }}{{ define "partials/p3" }}Inline: p3{{ end }} |
| 519 |
-- layouts/home.html -- |
| 520 |
{{ define "main" }} |
| 521 |
|
| 522 |
{{ $p1 := partial "p1" . }} |
| 523 |
{{ $p2 := partial "p2" . }} |
| 524 |
|
| 525 |
P1: {{ $p1 }} |
| 526 |
P2: {{ $p2 }} |
| 527 |
|
| 528 |
{{ end }} |
| 529 |
|
| 530 |
|
| 531 |
{{ define "partials/p1" }}Inline: p1{{ end }} |
| 532 |
|
| 533 |
{{ define "partials/p2" }} |
| 534 |
{{ $value := 32 }} |
| 535 |
{{ return $value }} |
| 536 |
{{ end }} |
| 537 |
` |
| 538 |
b := Test(t, files) |
| 539 |
|
| 540 |
b.AssertFileContent("public/index.html", |
| 541 |
` |
| 542 |
P1: Inline: p1 |
| 543 |
P2: 32 |
| 544 |
P3: Inline: p3 |
| 545 |
`, |
| 546 |
) |
| 547 |
} |
| 548 |
|
| 549 |
// https://github.com/gohugoio/hugo/issues/7478 |
| 550 |
func TestBaseWithAndWithoutDefine(t *testing.T) { |
| 551 |
t.Parallel() |
| 552 |
|
| 553 |
files := ` |
| 554 |
-- hugo.toml -- |
| 555 |
baseURL = "http://example.com/" |
| 556 |
-- content/p1.md -- |
| 557 |
--- |
| 558 |
title: P |
| 559 |
--- |
| 560 |
Content |
| 561 |
-- layouts/baseof.html -- |
| 562 |
::Header Start:{{ block "header" . }}{{ end }}:Header End: |
| 563 |
::{{ block "main" . }}Main{{ end }}:: |
| 564 |
-- layouts/home.html -- |
| 565 |
{{ define "header" }} |
| 566 |
Home Header |
| 567 |
{{ end }} |
| 568 |
{{ define "main" }} |
| 569 |
This is home main |
| 570 |
{{ end }} |
| 571 |
-- layouts/single.html -- |
| 572 |
{{ define "main" }} |
| 573 |
This is single main |
| 574 |
{{ end }} |
| 575 |
` |
| 576 |
b := Test(t, files) |
| 577 |
|
| 578 |
b.AssertFileContent("public/index.html", ` |
| 579 |
Home Header |
| 580 |
This is home main |
| 581 |
`, |
| 582 |
) |
| 583 |
|
| 584 |
b.AssertFileContent("public/p1/index.html", ` |
| 585 |
::Header Start::Header End: |
| 586 |
This is single main |
| 587 |
`, |
| 588 |
) |
| 589 |
} |
| 590 |
|
| 591 |
// Issue 9393. |
| 592 |
func TestApplyWithNamespace(t *testing.T) { |
| 593 |
t.Parallel() |
| 594 |
|
| 595 |
files := ` |
| 596 |
-- hugo.toml -- |
| 597 |
baseURL = "http://example.com/" |
| 598 |
-- content/p1.md -- |
| 599 |
-- layouts/home.html -- |
| 600 |
{{ $b := slice " a " " b " " c" }} |
| 601 |
{{ $a := apply $b "strings.Trim" "." " " }} |
| 602 |
a: {{ $a }} |
| 603 |
` |
| 604 |
b := Test(t, files) |
| 605 |
|
| 606 |
b.AssertFileContent("public/index.html", `a: [a b c]`) |
| 607 |
} |
| 608 |
|
| 609 |
// Legacy behavior for internal templates. |
| 610 |
func TestOverrideInternalTemplate(t *testing.T) { |
| 611 |
files := ` |
| 612 |
-- hugo.toml -- |
| 613 |
baseURL = "https://example.org" |
| 614 |
-- layouts/home.html -- |
| 615 |
{{ template "_internal/google_analytics_async.html" . }} |
| 616 |
-- layouts/_internal/google_analytics_async.html -- |
| 617 |
Overridden. |
| 618 |
` |
| 619 |
b := Test(t, files) |
| 620 |
|
| 621 |
b.AssertFileContent("public/index.html", "Overridden.") |
| 622 |
} |
| 623 |
|
| 624 |
// Issue 13877 |
| 625 |
func TestTemplateSelectionFirstMediaTypeSuffix(t *testing.T) { |
| 626 |
t.Parallel() |
| 627 |
|
| 628 |
files := ` |
| 629 |
-- hugo.toml -- |
| 630 |
disableKinds = ['home','rss','section','sitemap','taxonomy','term'] |
| 631 |
|
| 632 |
[mediaTypes.'text/html'] |
| 633 |
suffixes = ['b','a','d','c'] |
| 634 |
|
| 635 |
[outputFormats.html] |
| 636 |
mediaType = 'text/html' |
| 637 |
-- content/p1.md -- |
| 638 |
--- |
| 639 |
title: p1 |
| 640 |
--- |
| 641 |
-- layouts/page.html.a -- |
| 642 |
page.html.a |
| 643 |
-- layouts/page.html.b -- |
| 644 |
page.html.b |
| 645 |
-- layouts/page.html.c -- |
| 646 |
page.html.c |
| 647 |
-- layouts/page.html.d -- |
| 648 |
page.html.d |
| 649 |
` |
| 650 |
|
| 651 |
b := Test(t, files) |
| 652 |
|
| 653 |
b.AssertFileContent("public/p1/index.b", "page.html.b") |
| 654 |
} |