b.AssertFileContent("public/index.html", "d:truthy", "$")
}
+
+// Issue 14711
+// When using {{ with partial "name" . }} inside a <script> tag,
+// the injected else block's empty string return was being JS-escaped to "".
+func TestDecoratorPartialFalsyReturnInScript(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+disableKinds = ["section", "taxonomy", "term", "sitemap", "RSS"]
+-- content/p1.md --
+---
+title: "Page 1"
+---
+-- layouts/_partials/empty.html --
+{{ return "" }}
+-- layouts/home.html --
+<script>
+ {{- with partial "empty" . }}
+ "this-should-be-skipped": "{{ . }}"
+ {{- end }}
+</script>
+`
+ b := hugolib.Test(t, files)
+
+ b.AssertFileContent("public/index.html", "! \"\"")
+}
import (
"context"
"fmt"
+ htmltemplate "html/template"
"strconv"
"sync/atomic"
}
if !top.Bool {
// Prevents anything being rendered if inner was not called.
- return ""
+ // Use template.JS to avoid Go's html/template JS escaper
+ // wrapping an empty string in quotes inside <script> tags.
+ // See https://github.com/gohugoio/hugo/issues/14711
+ return htmltemplate.JS("")
}
return top.Bool // return whether inner exists in the wrapped partial.
}