| 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 templates_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
qt "github.com/frankban/quicktest" |
| 22 |
"github.com/gohugoio/hugo/hugolib" |
| 23 |
) |
| 24 |
|
| 25 |
func TestDecoratorInnerNeverCalled(t *testing.T) { |
| 26 |
t.Parallel() |
| 27 |
|
| 28 |
filesTemplate := ` |
| 29 |
-- hugo.toml -- |
| 30 |
disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"] |
| 31 |
-- content/p1.md -- |
| 32 |
--- |
| 33 |
title: "Page 1" |
| 34 |
--- |
| 35 |
-- content/p2.md -- |
| 36 |
--- |
| 37 |
title: "Page 2" |
| 38 |
--- |
| 39 |
-- layouts/_partials/cards.html -- |
| 40 |
Start:{{ range . }}{{ PLACEHOLDER . }}{{ end }}End$ |
| 41 |
-- layouts/home.html -- |
| 42 |
1:${{ with partial "cards.html" (site.RegularPages) }}{{ printf "Got %T" . }}|{{ end }}$ |
| 43 |
2:${{ with partial "cards.html" (site.RegularPages | first 0) }}{{ printf "Got %T" . }}|{{ end }}$ |
| 44 |
` |
| 45 |
|
| 46 |
for _, placeholder := range []string{"inner", "templates.Inner"} { |
| 47 |
files := strings.ReplaceAll(filesTemplate, "PLACEHOLDER", placeholder) |
| 48 |
b := hugolib.Test(t, files) |
| 49 |
|
| 50 |
b.AssertFileContent("public/index.html", |
| 51 |
"1:$Start:Got *hugolib.pageState|Got *hugolib.pageState|End$$", |
| 52 |
"2:$Start:End$$", |
| 53 |
) |
| 54 |
|
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func TestDecoratorInlinePartialInnerNeverCalled(t *testing.T) { |
| 59 |
t.Parallel() |
| 60 |
|
| 61 |
files := ` |
| 62 |
-- hugo.toml -- |
| 63 |
disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"] |
| 64 |
-- content/p1.md -- |
| 65 |
--- |
| 66 |
title: "Page 1" |
| 67 |
--- |
| 68 |
-- content/p2.md -- |
| 69 |
--- |
| 70 |
title: "Page 2" |
| 71 |
--- |
| 72 |
-- layouts/home.html -- |
| 73 |
${{ with partial "cards.html" (site.RegularPages) }}{{ printf "Got %T" . }}|{{ end }}$ |
| 74 |
{{ define "_partials/cards.html" }}Start:{{ range . }}{{ inner . }}{{ end }}End${{ end }} |
| 75 |
` |
| 76 |
|
| 77 |
b := hugolib.Test(t, files) |
| 78 |
|
| 79 |
b.AssertFileContent("public/index.html", |
| 80 |
"$Start:Got *hugolib.pageState|Got *hugolib.pageState|End$$", |
| 81 |
) |
| 82 |
} |
| 83 |
|
| 84 |
func TestDecoratorInlinePartial(t *testing.T) { |
| 85 |
t.Parallel() |
| 86 |
|
| 87 |
files := ` |
| 88 |
-- hugo.toml -- |
| 89 |
disableKinds = ["section", "taxonomy", "term", "sitemap", "rss"] |
| 90 |
-- layouts/home.html -- |
| 91 |
Home. |
| 92 |
{{ with partial "decorate.html" "Important!" }}Notice: {{ . }}{{ end }} |
| 93 |
{{ define "_partials/decorate.html" }}<b>{{ inner . }}</b>{{ end }} |
| 94 |
` |
| 95 |
b := hugolib.Test(t, files) |
| 96 |
|
| 97 |
b.AssertFileContent("public/index.html", "<b>Notice: Important!</b>") |
| 98 |
} |
| 99 |
|
| 100 |
func TestDecoratorNestedSimple(t *testing.T) { |
| 101 |
t.Parallel() |
| 102 |
|
| 103 |
files := ` |
| 104 |
-- hugo.toml -- |
| 105 |
disableKinds = ["section", "taxonomy", "term", "sitemap", "rss"] |
| 106 |
-- layouts/home.html -- |
| 107 |
Home. |
| 108 |
{{ with partial "a.html" "warning" }}{{ with partial "b.html" . }}{{ with partial "c.html" . }}{{ . }}{{ end }}{{ end }}{{ end }} |
| 109 |
-- layouts/_partials/a.html -- |
| 110 |
<a>{{ inner . }}</a> |
| 111 |
-- layouts/_partials/b.html -- |
| 112 |
<b>{{ inner . }}</b> |
| 113 |
-- layouts/_partials/c.html -- |
| 114 |
<c>{{ inner . }}</c> |
| 115 |
` |
| 116 |
b := hugolib.Test(t, files) |
| 117 |
|
| 118 |
b.AssertFileContent("public/index.html", "<a><b><c>warning</c></b></a>") |
| 119 |
} |
| 120 |
|
| 121 |
func TestDecoratorNested2(t *testing.T) { |
| 122 |
t.Parallel() |
| 123 |
|
| 124 |
files := ` |
| 125 |
-- hugo.toml -- |
| 126 |
disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"] |
| 127 |
title = "Test title" |
| 128 |
-- content/p1.md -- |
| 129 |
--- |
| 130 |
title: "Page 1" |
| 131 |
--- |
| 132 |
-- content/p2.md -- |
| 133 |
--- |
| 134 |
title: "Page 2" |
| 135 |
--- |
| 136 |
-- layouts/page.html -- |
| 137 |
{{ .Title }} |
| 138 |
-- layouts/home.html -- |
| 139 |
{{ $pages := site.RegularPages }} |
| 140 |
{{ with partial "ul.html" $pages }}<a href="{{ .RelPermalink }}">{{ with partial "bold.html" . }}<span>{{ .LinkTitle }}</span>{{ end }}</a>{{ end }} |
| 141 |
-- layouts/_partials/ul.html -- |
| 142 |
<ul> |
| 143 |
{{- range . }} |
| 144 |
<li>{{ inner . }}</li> |
| 145 |
{{- end }} |
| 146 |
</ul> |
| 147 |
-- layouts/_partials/bold.html -- |
| 148 |
<b>{{ inner $ }}</b> |
| 149 |
` |
| 150 |
|
| 151 |
b, err := hugolib.TestE(t, files) |
| 152 |
|
| 153 |
b.Assert(err, qt.IsNil) |
| 154 |
b.AssertFileContent("public/index.html", ` |
| 155 |
<ul> |
| 156 |
<li><a href="/p1/"><b><span>Page 1</span></b></a></li> |
| 157 |
<li><a href="/p2/"><b><span>Page 2</span></b></a></li> |
| 158 |
</ul> |
| 159 |
`) |
| 160 |
} |
| 161 |
|
| 162 |
func TestDecoratorMultiple(t *testing.T) { |
| 163 |
t.Parallel() |
| 164 |
|
| 165 |
files := ` |
| 166 |
-- hugo.toml -- |
| 167 |
title = "Test title" |
| 168 |
-- layouts/home.html -- |
| 169 |
{{ with partial "d1.html" . }}X2{{ . }}X4{{ end }} |
| 170 |
-- layouts/_partials/d1.html -- |
| 171 |
X1{{ inner "X3" }}X5 |
| 172 |
{{ with partial "d2.html" . }}X7{{ . }}X9{{ end }} |
| 173 |
{{ with partial "noinner.html" "N3" }}N1{{ . }}N5{{ end }} |
| 174 |
X14{{ inner "X15" }}X16 |
| 175 |
-- layouts/_partials/d2.html -- |
| 176 |
X6{{ inner "X8" }}X10 |
| 177 |
{{ with partial "d3.html" . }}A1{{ . }}A2{{ end }} |
| 178 |
X11{{ inner "X12" }}X13 |
| 179 |
-- layouts/_partials/d3.html -- |
| 180 |
A3{{ inner "A4" }}A5 |
| 181 |
A6{{ inner "A7" }}A8 |
| 182 |
-- layouts/_partials/noinner.html -- |
| 183 |
N2{{ . }}N4 |
| 184 |
` |
| 185 |
|
| 186 |
b := hugolib.Test(t, files) |
| 187 |
|
| 188 |
b.AssertFileContent("public/index.html", |
| 189 |
"X1X2X3X4X5", |
| 190 |
"X6X7X8X9X10", |
| 191 |
"X11X7X12X9X13", |
| 192 |
"X14X2X15X4X16", |
| 193 |
"A3A1A4A2A5", |
| 194 |
"N1N2N3N4N5", // partial with with, but no inner. |
| 195 |
) |
| 196 |
} |
| 197 |
|
| 198 |
func TestDecoratorEditInner(t *testing.T) { |
| 199 |
t.Parallel() |
| 200 |
|
| 201 |
files := ` |
| 202 |
-- hugo.toml -- |
| 203 |
baseURL = "http://example.org/" |
| 204 |
disableLiveReload = true |
| 205 |
-- layouts/_partials/a.html -- |
| 206 |
<b>{{ inner . }}</b> |
| 207 |
-- layouts/home.html -- |
| 208 |
{{ with partial "a.html" "Hello" }}{{ . }} World0{{ end }}$ |
| 209 |
` |
| 210 |
b := hugolib.TestRunning(t, files) |
| 211 |
|
| 212 |
b.AssertFileContent("public/index.html", |
| 213 |
"<b>Hello World0</b>$", |
| 214 |
) |
| 215 |
|
| 216 |
for i := range 4 { |
| 217 |
b.EditFileReplaceAll("layouts/home.html", fmt.Sprintf("World%d", i), fmt.Sprintf("World%d", i+1)).Build() |
| 218 |
|
| 219 |
b.AssertFileContent("public/index.html") |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func TestDecoratorEditPartial(t *testing.T) { |
| 224 |
t.Parallel() |
| 225 |
|
| 226 |
files := ` |
| 227 |
-- hugo.toml -- |
| 228 |
baseURL = "http://example.org/" |
| 229 |
disableLiveReload = true |
| 230 |
-- layouts/_partials/a.html -- |
| 231 |
<b>{{ inner (printf "%s World0" .) }}</b> |
| 232 |
-- layouts/home.html -- |
| 233 |
{{ with partial "a.html" "Hello" }}{{ . }}{{ end }}$ |
| 234 |
` |
| 235 |
b := hugolib.TestRunning(t, files) |
| 236 |
|
| 237 |
b.AssertFileContent("public/index.html", |
| 238 |
"<b>Hello World0</b>$", |
| 239 |
) |
| 240 |
|
| 241 |
for i := range 4 { |
| 242 |
b.EditFileReplaceAll("layouts/_partials/a.html", fmt.Sprintf("World%d", i), fmt.Sprintf("World%d", i+1)).Build() |
| 243 |
|
| 244 |
b.AssertFileContent("public/index.html") |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func TestDecoratorDuplicateInner(t *testing.T) { |
| 249 |
t.Parallel() |
| 250 |
|
| 251 |
files := ` |
| 252 |
-- hugo.toml -- |
| 253 |
-- layouts/_partials/a.html -- |
| 254 |
<b>{{ inner . }}</b> |
| 255 |
-- layouts/home.html -- |
| 256 |
1: {{ with partial "a.html" "Hello" }}{{ . }}{{ end }}$ |
| 257 |
2: {{ with partial "a.html" "World" }}{{ . }}{{ end }}$ |
| 258 |
|
| 259 |
` |
| 260 |
b := hugolib.Test(t, files) |
| 261 |
|
| 262 |
b.AssertFileContent("public/index.html", |
| 263 |
"1: <b>Hello</b>$", |
| 264 |
"2: <b>World</b>$", |
| 265 |
) |
| 266 |
} |
| 267 |
|
| 268 |
func TestDecoratorInAllTemplateTypes(t *testing.T) { |
| 269 |
t.Parallel() |
| 270 |
|
| 271 |
files := ` |
| 272 |
-- hugo.toml -- |
| 273 |
-- layouts/_partials/b.html -- |
| 274 |
<b>{{ inner . }}</b> |
| 275 |
-- layouts/_markup/render-link.html -- |
| 276 |
{{ with partial "b.html" "hello" }}{{ . }} world{{ end }} |
| 277 |
-- layouts/_shortcodes/a.html -- |
| 278 |
{{ with partial "b.html" (.Get 0) }}{{ . }} world{{ end }} |
| 279 |
-- layouts/_partials/a.html -- |
| 280 |
{{ with partial "b.html" . }}{{ . }} world{{ end }} |
| 281 |
-- layouts/home.html -- |
| 282 |
partial: {{ partial "a.html" "hello" }}$ |
| 283 |
|
| 284 |
{{ .Content}} |
| 285 |
-- content/_index.md -- |
| 286 |
--- |
| 287 |
title: "Home" |
| 288 |
--- |
| 289 |
shortcode: {{< a "hello" >}}$ |
| 290 |
link: [example](/some-url)$ |
| 291 |
` |
| 292 |
b := hugolib.Test(t, files) |
| 293 |
|
| 294 |
b.AssertFileContent("public/index.html", |
| 295 |
"partial: <b>hello world</b>$", |
| 296 |
"shortcode: <b>hello world</b>$", |
| 297 |
"link: <b>hello world</b>$</p>", |
| 298 |
) |
| 299 |
} |
| 300 |
|
| 301 |
func TestDecoratorInAllPartialFuncNames(t *testing.T) { |
| 302 |
t.Parallel() |
| 303 |
|
| 304 |
filesTemplate := ` |
| 305 |
-- hugo.toml -- |
| 306 |
-- layouts/_partials/b.html -- |
| 307 |
<b>{{ inner . }}</b> |
| 308 |
-- layouts/home.html -- |
| 309 |
{{ with FUNC "b.html" "hello" }}{{ . }} world{{ end }}$ |
| 310 |
` |
| 311 |
|
| 312 |
for _, partialFunc := range []string{"partial", "partialCached", "partials.Include", "partials.IncludeCached"} { |
| 313 |
files := strings.ReplaceAll(filesTemplate, "FUNC", partialFunc) |
| 314 |
b := hugolib.Test(t, files) |
| 315 |
|
| 316 |
b.AssertFileContent("public/index.html", |
| 317 |
"<b>hello world</b>$", |
| 318 |
) |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
func TestDecoratorReturn(t *testing.T) { |
| 323 |
t.Parallel() |
| 324 |
|
| 325 |
files := ` |
| 326 |
-- hugo.toml -- |
| 327 |
-- layouts/_partials/add.html -- |
| 328 |
{{ $sum := .sum }} |
| 329 |
{{ $sum = add $sum (inner 1) }} |
| 330 |
{{ $sum = add $sum (inner 2) }} |
| 331 |
{{ $sum = add $sum (inner 3) }} |
| 332 |
{{ return $sum }} |
| 333 |
-- layouts/home.html -- |
| 334 |
{{ $v := dict "sum" 1 }} |
| 335 |
Sum: {{ with partial "add.html" $v }} |
| 336 |
{{ $sum := mul . 2 }} |
| 337 |
{{ return $sum }} |
| 338 |
{{ end }}$ |
| 339 |
` |
| 340 |
b := hugolib.Test(t, files) |
| 341 |
|
| 342 |
// .sum = 1 |
| 343 |
// inner 1 => 2 |
| 344 |
// inner 2 => 4 |
| 345 |
// inner 3 => 6 |
| 346 |
// 1 + 2 + 4 + 6 = 13 |
| 347 |
b.AssertFileContent("public/index.html", "Sum: 13$") |
| 348 |
} |
| 349 |
|
| 350 |
func TestDecoratorFailOnInnerInWith(t *testing.T) { |
| 351 |
t.Parallel() |
| 352 |
|
| 353 |
filesTemplate := ` |
| 354 |
-- hugo.toml -- |
| 355 |
-- layouts/_partials/b.html -- |
| 356 |
<b>{{ inner . }}</b> |
| 357 |
-- layouts/home.html -- |
| 358 |
{{ with partial "b.html" "hello" }} |
| 359 |
This construct creates a loop: {{PLACEHOLDER . }} |
| 360 |
{{ end }}$ |
| 361 |
` |
| 362 |
for _, placeholder := range []string{"inner", "templates.Inner", " inner", "\ninner"} { |
| 363 |
files := strings.ReplaceAll(filesTemplate, "PLACEHOLDER", placeholder) |
| 364 |
b, err := hugolib.TestE(t, files) |
| 365 |
|
| 366 |
b.Assert(err, qt.Not(qt.IsNil)) |
| 367 |
b.Assert(err.Error(), qt.Contains, "inner cannot be used inside a with block that wraps a partial decorator") |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
func TestPartialDecoratorInParens(t *testing.T) { |
| 372 |
t.Parallel() |
| 373 |
|
| 374 |
files := ` |
| 375 |
-- hugo.toml -- |
| 376 |
-- layouts/home.html -- |
| 377 |
Home. |
| 378 |
{{ define "_partials/b.html" }} |
| 379 |
<b>{{ inner . }}</b> |
| 380 |
{{ end }} |
| 381 |
{{ with (partial "b.html" "Important!") }}Notice: {{ . }}{{ end }} |
| 382 |
` |
| 383 |
|
| 384 |
b := hugolib.Test(t, files) |
| 385 |
|
| 386 |
b.AssertFileContent("public/index.html", "<b>Notice: Important!</b>") |
| 387 |
} |
| 388 |
|
| 389 |
func TestPartialDecoratorBreakInWith(t *testing.T) { |
| 390 |
t.Parallel() |
| 391 |
|
| 392 |
filesTemplate := ` |
| 393 |
-- hugo.toml -- |
| 394 |
-- layouts/home.html -- |
| 395 |
Home. |
| 396 |
{{ define "_partials/b.html" }} |
| 397 |
<b>{{ inner . }}</b> |
| 398 |
{{ end }} |
| 399 |
{{ with (partial "b.html" "Important!") }} |
| 400 |
{{ range seq 1 5 }} |
| 401 |
{{ if eq . 3 }} |
| 402 |
PLACEHOLDER |
| 403 |
{{ end }} |
| 404 |
Notice: {{ . }} |
| 405 |
{{ end }} |
| 406 |
{{ end }} |
| 407 |
` |
| 408 |
|
| 409 |
for _, placeholder := range []string{"{{ break }}", "{{ continue }}"} { |
| 410 |
files := strings.ReplaceAll(filesTemplate, "PLACEHOLDER", placeholder) |
| 411 |
b := hugolib.Test(t, files) |
| 412 |
b.AssertFileContent("public/index.html", "Notice: 1", "Notice: 2", "! Notice: 3") |
| 413 |
if strings.Contains(placeholder, "continue") { |
| 414 |
b.AssertFileContent("public/index.html", "Notice: 4", "Notice: 5") |
| 415 |
} else { |
| 416 |
b.AssertFileContent("public/index.html", "! Notice: 4", "! Notice: 5") |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
func TestPartialWithBreakOutsideRange14333(t *testing.T) { |
| 422 |
t.Parallel() |
| 423 |
|
| 424 |
filesTemplate := ` |
| 425 |
-- hugo.toml -- |
| 426 |
-- layouts/home.html -- |
| 427 |
Home. {{ partial "a" }}:Done. |
| 428 |
{{- define "_partials/a" }} |
| 429 |
{{- $items := slice "a" "b" "c" }} |
| 430 |
{{- range $items }} |
| 431 |
{{- with partial "b" . -}} |
| 432 |
PLACEHOLDER |
| 433 |
{{- else }} |
| 434 |
else: {{ . -}} |
| 435 |
{{- end }} |
| 436 |
{{- end }} |
| 437 |
{{- end }} |
| 438 |
{{- define "_partials/b" }} |
| 439 |
{{ $b := true }} |
| 440 |
{{ if ne . "b" }} |
| 441 |
{{ $b = false }} |
| 442 |
{{ end }} |
| 443 |
{{ return $b }} |
| 444 |
{{ end }} |
| 445 |
` |
| 446 |
for _, placeholder := range []string{"{{ break }}", "{{ continue }}", "{{- break }}"} { |
| 447 |
files := strings.ReplaceAll(filesTemplate, "PLACEHOLDER", placeholder) |
| 448 |
b := hugolib.Test(t, files) |
| 449 |
if strings.Contains(placeholder, "continue") { |
| 450 |
b.AssertFileContent("public/index.html", "else: c:Done.") |
| 451 |
} else { |
| 452 |
b.AssertFileContent("public/index.html", "else: a:Done.") |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
func TestDecoratorFingerprintScript(t *testing.T) { |
| 458 |
t.Parallel() |
| 459 |
|
| 460 |
files := ` |
| 461 |
-- hugo.toml -- |
| 462 |
baseURL = "https://example.org/" |
| 463 |
[outputs] |
| 464 |
home = ["html", "headers"] |
| 465 |
[mediaTypes] |
| 466 |
[mediaTypes."text/netlify"] |
| 467 |
delimiter = "" |
| 468 |
[outputFormats] |
| 469 |
[outputFormats.headers] |
| 470 |
baseName = "_headers" |
| 471 |
isPlainText = true |
| 472 |
mediatype = "text/netlify" |
| 473 |
notAlternative = true |
| 474 |
-- layouts/_partials/script.html -- |
| 475 |
{{ $s := (templates.Inner .) }} |
| 476 |
{{ $r := resources.FromString ($s | xxhash) $s | fingerprint }} |
| 477 |
{{ $hash := $s | crypto.SHA256 | encoding.HexDecode | encoding.Base64Encode }} |
| 478 |
{{ $sri := (printf "sha256-%s" $hash) }} |
| 479 |
{{ if ne $sri $r.Data.Integrity }} |
| 480 |
{{ errorf "SRI hash mismatch: %s != %s" $sri $r.Data.Integrity }} |
| 481 |
{{ end }} |
| 482 |
{{ hugo.Store.SetInMap "srihashes" $sri true }} |
| 483 |
<script>{{ $s | safeJS }}</script> |
| 484 |
-- layouts/home.html -- |
| 485 |
Home. |
| 486 |
{{ with partial "script.html" . }} |
| 487 |
console.log("Hello world"); |
| 488 |
{{ end }} |
| 489 |
{{ with partial "script.html" . }} |
| 490 |
console.log("Hugo rocks!"); |
| 491 |
{{ end }} |
| 492 |
-- layouts/home.headers -- |
| 493 |
{{ $hashes := hugo.Store.Get "srihashes" -}} |
| 494 |
Content-Security-Policy: script-src 'self'{{ range $k, $v := $hashes }} '{{ $k }}'{{ end }}; |
| 495 |
` |
| 496 |
|
| 497 |
b := hugolib.Test(t, files) |
| 498 |
|
| 499 |
b.AssertFileContentExact("public/index.html", "<script>\nconsole.log(\"Hello world\");\n</script>") |
| 500 |
b.AssertFileContentExact("public/_headers", "Content-Security-Policy: script-src 'self' 'sha256-t6z9j4/CagmaDDbn6nHbntzt8PRk8HZ1TwrgdYuZjbE=' 'sha256-wVbS8f78ttoSSGU80KghGoPcTO8DXqT0Uc/87a7zMdk=';") |
| 501 |
} |