Skip to content

Commit

Permalink
templates: Fix handling of multiple defers in the same template
Browse files Browse the repository at this point in the history
Fixes #13236
  • Loading branch information
bep committed Jan 9, 2025
1 parent b13d0a6 commit 5246d6f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions hugolib/hugo_sites_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tpl/templates/defer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}

0 comments on commit 5246d6f

Please sign in to comment.