From cd07e6d57b158a76f812e8c4c9567dbc84f57939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 21 Nov 2019 19:45:03 +0100 Subject: [PATCH] Fix GetPage Params case issue Fixes #5946 --- hugolib/case_insensitive_test.go | 5 +++ hugolib/testhelpers_test.go | 2 +- tpl/tplimpl/template_ast_transformers.go | 3 +- tpl/tplimpl/template_ast_transformers_test.go | 44 +++++++++++++++++-- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/hugolib/case_insensitive_test.go b/hugolib/case_insensitive_test.go index bffffb6be..5c7dbe073 100644 --- a/hugolib/case_insensitive_test.go +++ b/hugolib/case_insensitive_test.go @@ -179,6 +179,10 @@ Site Title: {{ .Site.Title }} Site Lang Mood: {{ .Site.Language.Params.MOoD }} Page Colors: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }} Site Colors: {{ .Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }} +{{ $page2 := .Site.GetPage "/sect2/page2" }} +{{ if $page2 }} +Page2: {{ $page2.Params.ColoR }} +{{ end }} {{ .Content }} {{ partial "partial.html" . }} `) @@ -207,6 +211,7 @@ Site Colors: {{ .Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }} "Page Title: Side 1", "Site Title: Nynorsk title", "«Hi»", // angled quotes + "Page2: black ", ) th.assertFileContent(filepath.Join("public", "en", "sect1", "page1", "index.html"), diff --git a/hugolib/testhelpers_test.go b/hugolib/testhelpers_test.go index 00f802359..08949a1fd 100644 --- a/hugolib/testhelpers_test.go +++ b/hugolib/testhelpers_test.go @@ -743,7 +743,7 @@ func (th testHelper) assertFileContent(filename string, matches ...string) { content := readDestination(th, th.Fs, filename) for _, match := range matches { match = th.replaceDefaultContentLanguageValue(match) - th.Assert(strings.Contains(content, match), qt.Equals, true) + th.Assert(strings.Contains(content, match), qt.Equals, true, qt.Commentf(content)) } } diff --git a/tpl/tplimpl/template_ast_transformers.go b/tpl/tplimpl/template_ast_transformers.go index fb0728a63..d257d7a31 100644 --- a/tpl/tplimpl/template_ast_transformers.go +++ b/tpl/tplimpl/template_ast_transformers.go @@ -508,8 +508,7 @@ func (d decl) resolveVariables(idents []string) ([]string, bool) { } if !d.isKeyword(replacement) { - // This can not be .Site.Params etc. - return nil, false + continue } replacement = strings.TrimPrefix(replacement, ".") diff --git a/tpl/tplimpl/template_ast_transformers_test.go b/tpl/tplimpl/template_ast_transformers_test.go index 39b5c460a..682af2772 100644 --- a/tpl/tplimpl/template_ast_transformers_test.go +++ b/tpl/tplimpl/template_ast_transformers_test.go @@ -26,6 +26,19 @@ import ( qt "github.com/frankban/quicktest" ) +type paramsHolder struct { + params map[string]interface{} + page *paramsHolder +} + +func (p paramsHolder) Params() map[string]interface{} { + return p.params +} + +func (p paramsHolder) GetPage(arg string) *paramsHolder { + return p.page +} + var ( testFuncs = map[string]interface{}{ "getif": func(v interface{}) interface{} { return v }, @@ -37,16 +50,22 @@ var ( "ByWeight": fmt.Sprintf("%v:%v:%v", seq, key, args), }, nil }, - "site": func() interface{} { - return map[string]interface{}{ - "Params": map[string]interface{}{ + "site": func() paramsHolder { + return paramsHolder{ + params: map[string]interface{}{ "lower": "global-site", }, + page: ¶msHolder{ + params: map[string]interface{}{ + "lower": "page", + }, + }, } }, } paramsData = map[string]interface{}{ + "NotParam": "Hi There", "Slice": []int{1, 3}, "Params": map[string]interface{}{ @@ -81,6 +100,16 @@ var ( }, }, }, + "Site2": paramsHolder{ + params: map[string]interface{}{ + "lower": "global-site", + }, + page: ¶msHolder{ + params: map[string]interface{}{ + "lower": "page", + }, + }, + }, } paramsTempl = ` @@ -170,6 +199,11 @@ PARAMS SITE GLOBAL1: {{ site.Params.LOwER }} {{ $site := site }} PARAMS SITE GLOBAL2: {{ $lower }} PARAMS SITE GLOBAL3: {{ $site.Params.LOWER }} + +{{ $p := $site.GetPage "foo" }} +PARAMS GETPAGE: {{ $p.Params.LOWER }} +{{ $p := .Site2.GetPage "foo" }} +PARAMS GETPAGE2: {{ $p.Params.LOWER }} ` ) @@ -248,6 +282,10 @@ func TestParamsKeysToLower(t *testing.T) { c.Assert(result, qt.Contains, "PARAMS SITE GLOBAL2: global-site") c.Assert(result, qt.Contains, "PARAMS SITE GLOBAL3: global-site") + // + c.Assert(result, qt.Contains, "PARAMS GETPAGE: page") + c.Assert(result, qt.Contains, "PARAMS GETPAGE2: page") + } func BenchmarkTemplateParamsKeysToLower(b *testing.B) {