Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43aad71

Browse files
bepclaude
andcommitted
tpl: Fix stray quotes from partial decorator in script context
Use template.JS for the falsy return value of _PopPartialDecorator so Go's html/template JS escaper doesn't wrap the empty string in quotes inside <script> tags. Fixes #14711 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 9f1f1be commit 43aad71

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

tpl/templates/decorator_falsy_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,30 @@ title: "Page 1"
6161

6262
b.AssertFileContent("public/index.html", "d:truthy", "$")
6363
}
64+
65+
// Issue 14711
66+
// When using {{ with partial "name" . }} inside a <script> tag,
67+
// the injected else block's empty string return was being JS-escaped to "".
68+
func TestDecoratorPartialFalsyReturnInScript(t *testing.T) {
69+
t.Parallel()
70+
71+
files := `
72+
-- hugo.toml --
73+
disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"]
74+
-- content/p1.md --
75+
---
76+
title: "Page 1"
77+
---
78+
-- layouts/_partials/empty.html --
79+
{{ return "" }}
80+
-- layouts/home.html --
81+
<script>
82+
{{- with partial "empty" . }}
83+
"this-should-be-skipped": "{{ . }}"
84+
{{- end }}
85+
</script>
86+
`
87+
b := hugolib.Test(t, files)
88+
89+
b.AssertFileContent("public/index.html", "! \"\"")
90+
}

tpl/templates/templates.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package templates
1717
import (
1818
"context"
1919
"fmt"
20+
htmltemplate "html/template"
2021
"strconv"
2122
"sync/atomic"
2223

@@ -113,7 +114,10 @@ func (ns *Namespace) _PopPartialDecorator(ctx context.Context, id string) any {
113114
}
114115
if !top.Bool {
115116
// Prevents anything being rendered if inner was not called.
116-
return ""
117+
// Use template.JS to avoid Go's html/template JS escaper
118+
// wrapping an empty string in quotes inside <script> tags.
119+
// See https://github.com/gohugoio/hugo/issues/14711
120+
return htmltemplate.JS("")
117121
}
118122
return top.Bool // return whether inner exists in the wrapped partial.
119123
}

0 commit comments

Comments
 (0)