]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/collections: Fix and deprecate echoParams
authorJoe Mooring <joe.mooring@veriphor.com>
Tue, 26 Sep 2023 20:49:44 +0000 (13:49 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 27 Sep 2023 15:34:13 +0000 (17:34 +0200)
Fixes #11498

tpl/collections/collections.go
tpl/collections/integration_test.go

index 92aa2b9e6bbff46aef9d313edfd7a98e586c0cf7..2f7eb745bbab1cc9d5bd50cb4e7684e796c6b128 100644 (file)
@@ -31,6 +31,7 @@ import (
        "github.com/gohugoio/hugo/common/maps"
        "github.com/gohugoio/hugo/common/types"
        "github.com/gohugoio/hugo/deps"
+       "github.com/gohugoio/hugo/helpers"
        "github.com/gohugoio/hugo/langs"
        "github.com/gohugoio/hugo/tpl/compare"
        "github.com/spf13/cast"
@@ -195,9 +196,11 @@ func (ns *Namespace) Dictionary(values ...any) (map[string]any, error) {
        return root, nil
 }
 
-// EchoParam returns the value in the collection c with key k if is set; otherwise, it returns an
+// EchoParam returns the value in the collection c with key k if is set; otherwise, it returns an
 // empty string.
+// Deprecated: Use the index function instead.
 func (ns *Namespace) EchoParam(c, k any) any {
+       helpers.Deprecated("collections.EchoParam", "Use the index function instead.", false)
        av, isNil := indirect(reflect.ValueOf(c))
        if isNil {
                return ""
@@ -233,6 +236,8 @@ func (ns *Namespace) EchoParam(c, k any) any {
                        return avv.Float()
                case reflect.String:
                        return avv.String()
+               case reflect.Bool:
+                       return avv.Bool()
                }
        }
 
index a059bfd26bee2efdb66e58893591f5658cce0d8c..a443755f855be67b531ffe288df8cb0d97c88042 100644 (file)
@@ -231,3 +231,37 @@ foo: bc
        ).Build()
        b.AssertFileContent("public/index.html", "<ul><li>P1</li><li>P2</li></ul>")
 }
+
+// Issue #11498
+func TestEchoParams(t *testing.T) {
+       t.Parallel()
+       files := `
+-- hugo.toml --
+[params.footer]
+string = 'foo'
+int = 42
+float = 3.1415
+boolt = true
+boolf = false
+-- layouts/index.html --
+{{ echoParam .Site.Params.footer "string" }}
+{{ echoParam .Site.Params.footer "int" }}
+{{ echoParam .Site.Params.footer "float" }}
+{{ echoParam .Site.Params.footer "boolt" }}
+{{ echoParam .Site.Params.footer "boolf" }}
+       `
+
+       b := hugolib.NewIntegrationTestBuilder(
+               hugolib.IntegrationTestConfig{
+                       T:           t,
+                       TxtarString: files,
+               },
+       ).Build()
+       b.AssertFileContent("public/index.html",
+               "foo",
+               "42",
+               "3.1415",
+               "true",
+               "false",
+       )
+}