Debian Codegraph @

hugolib/template_test.go

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