Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-12-19 12:25:53 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-12-19 15:19:22 +0300
commit64b6b290751df01c47ff8d8fe21a3eca7a5db283 (patch)
tree5ed50ff7a2ce6475e65591c9fdabd02ce96fac76 /tpl/tplimpl
parentbb9c2988f871ca5fe6af9c8e207ec852c631c3b3 (diff)
tpl: Fix case handling in cast params
Fixes #5538
Diffstat (limited to 'tpl/tplimpl')
-rw-r--r--tpl/tplimpl/template_ast_transformers.go8
-rw-r--r--tpl/tplimpl/template_ast_transformers_test.go24
2 files changed, 23 insertions, 9 deletions
diff --git a/tpl/tplimpl/template_ast_transformers.go b/tpl/tplimpl/template_ast_transformers.go
index 9faaa2249..1e1589308 100644
--- a/tpl/tplimpl/template_ast_transformers.go
+++ b/tpl/tplimpl/template_ast_transformers.go
@@ -112,11 +112,9 @@ func (c *templateContext) paramsKeysToLower(n parse.Node) {
c.paramsKeysToLowerForNodes(subTempl.Root)
}
case *parse.PipeNode:
- for i, elem := range x.Decl {
- if len(x.Cmds) > i {
- // maps $site => .Site etc.
- c.decl[elem.Ident[0]] = x.Cmds[i].String()
- }
+ if len(x.Decl) == 1 && len(x.Cmds) == 1 {
+ // maps $site => .Site etc.
+ c.decl[x.Decl[0].Ident[0]] = x.Cmds[0].String()
}
for _, cmd := range x.Cmds {
diff --git a/tpl/tplimpl/template_ast_transformers_test.go b/tpl/tplimpl/template_ast_transformers_test.go
index 46a037cec..52d8c17b1 100644
--- a/tpl/tplimpl/template_ast_transformers_test.go
+++ b/tpl/tplimpl/template_ast_transformers_test.go
@@ -19,13 +19,16 @@ import (
"html/template"
+ "github.com/spf13/cast"
+
"github.com/stretchr/testify/require"
)
var (
testFuncs = map[string]interface{}{
- "First": func(v ...interface{}) interface{} { return v[0] },
- "Echo": func(v interface{}) interface{} { return v },
+ "ToTime": func(v interface{}) interface{} { return cast.ToTime(v) },
+ "First": func(v ...interface{}) interface{} { return v[0] },
+ "Echo": func(v interface{}) interface{} { return v },
"where": func(seq, key interface{}, args ...interface{}) (interface{}, error) {
return map[string]interface{}{
"ByWeight": fmt.Sprintf("%v:%v:%v", seq, key, args),
@@ -37,8 +40,9 @@ var (
"NotParam": "Hi There",
"Slice": []int{1, 3},
"Params": map[string]interface{}{
- "lower": "P1L",
- "slice": []int{1, 3},
+ "lower": "P1L",
+ "slice": []int{1, 3},
+ "mydate": "1972-01-28",
},
"Pages": map[string]interface{}{
"ByWeight": []int{1, 3},
@@ -142,6 +146,14 @@ PARAMS STRING2: {{ with $pages }}{{ .ByWeight }}{{ end }}
PARAMS STRING3: {{ $pages3.ByWeight }}
{{ $first := First .Pages .Site.Params.LOWER }}
PARAMS COMPOSITE: {{ $first.ByWeight }}
+
+
+{{ $time := $.Params.MyDate | ToTime }}
+{{ $time = $time.AddDate 0 1 0 }}
+PARAMS TIME: {{ $time.Format "2006-01-02" }}
+
+{{ $_x := $.Params.MyDate | ToTime }}
+PARAMS TIME2: {{ $_x.AddDate 0 1 0 }}
`
)
@@ -209,6 +221,10 @@ func TestParamsKeysToLower(t *testing.T) {
// Issue #5068
require.Contains(t, result, "PCurrentSection: pcurrentsection")
+ // Issue #5541
+ require.Contains(t, result, "PARAMS TIME: 1972-02-28")
+ require.Contains(t, result, "PARAMS TIME2: 1972-02-28")
+
}
func BenchmarkTemplateParamsKeysToLower(b *testing.B) {