From 61d3d20129e69be6730132b9ada5f8128d9f7233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 9 Jan 2025 04:09:25 +0100 Subject: [PATCH] templates: Fix handling of multiple defers in the same template Fixes #13236 --- hugolib/hugo_sites_build.go | 1 + tpl/templates/defer_integration_test.go | 27 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go index 60fb8ecc0..1518e2db8 100644 --- a/hugolib/hugo_sites_build.go +++ b/hugolib/hugo_sites_build.go @@ -508,6 +508,7 @@ func (s *Site) executeDeferredTemplates(de *deps.DeferredExecutions) error { } content = append(content[:low], append([]byte(deferred.Result), content[high:]...)...) + forward = len(deferred.Result) changed = true return nil diff --git a/tpl/templates/defer_integration_test.go b/tpl/templates/defer_integration_test.go index 8b06e7722..77be91cee 100644 --- a/tpl/templates/defer_integration_test.go +++ b/tpl/templates/defer_integration_test.go @@ -220,3 +220,30 @@ Home b.Assert(err, qt.Not(qt.IsNil)) b.Assert(err.Error(), qt.Contains, "resources.PostProcess cannot be used in a deferred template") } + +// Issue #13236. +func TestDeferMultipleInSameTemplate(t *testing.T) { + t.Parallel() + + files := ` +-- hugo.toml -- +-- layouts/index.html -- +Home. +... +{{ with (templates.Defer (dict "data" (dict "a" "b") )) }} + Defer 1 +{{ end }} +... +{{ with (templates.Defer (dict "data" (dict "a" "c") )) }} +Defer 2 +{{ end }} +{{ with (templates.Defer (dict "data" (dict "a" "d") )) }} +Defer 3 +{{ end }}{{ with (templates.Defer (dict "data" (dict "a" "d") )) }}{{ end }} +End. +` + + b := hugolib.Test(t, files) + + b.AssertFileContent("public/index.html", "Home.", "Defer 1", "Defer 2", "Defer 3", "End.") +}