ref: 96f09659ce8752c32a2a6429c9faf23be4faa091
parent: 03b369e6726ed8a732c07db48f7209425c434bbe
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Tue Nov 26 04:44:31 EST 2019
Fix language handling in ExecuteAsTemplate Fixes #6331
--- a/hugolib/hugo_sites_build_test.go
+++ b/hugolib/hugo_sites_build_test.go
@@ -1445,5 +1445,17 @@
PNG Data
`)
+ i18nContent := func(id, value string) string {
+ return fmt.Sprintf(`
+[%s]
+other = %q
+`, id, value)
+ }
+
+ b.WithSourceFile("i18n/en.toml", i18nContent("hello", "Hello"))
+ b.WithSourceFile("i18n/fr.toml", i18nContent("hello", "Bonjour"))
+ b.WithSourceFile("i18n/nb.toml", i18nContent("hello", "Hallo"))
+ b.WithSourceFile("i18n/nn.toml", i18nContent("hello", "Hallo"))
+
return &multiSiteTestBuilder{sitesBuilder: b, configFormat: configFormat, config: config, configData: configData}
}
--- a/hugolib/resource_chain_test.go
+++ b/hugolib/resource_chain_test.go
@@ -418,8 +418,7 @@
}},
{"execute-as-template", func() bool {
- // TODO(bep) eventually remove
- return isGo111()
+ return true
}, func(b *sitesBuilder) {
b.WithTemplates("home.html", `
{{ $var := "Hugo Page" }}
@@ -667,4 +666,31 @@
b.AssertFileContent("public/index.html",
"JSON: /jsons/data1.json: json1 content",
"JSONS: 2", "/jsons/data1.json: json1 content")
+}
+
+func TestExecuteAsTemplateWithLanguage(t *testing.T) {
+ b := newMultiSiteTestDefaultBuilder(t)
+ indexContent := `
+Lang: {{ site.Language.Lang }}
+{{ $templ := "{{T \"hello\"}}" | resources.FromString "f1.html" }}
+{{ $helloResource := $templ | resources.ExecuteAsTemplate (print "f%s.html" .Lang) . }}
+Hello1: {{T "hello"}}
+Hello2: {{ $helloResource.Content }}
+LangURL: {{ relLangURL "foo" }}
+`
+ b.WithTemplatesAdded("index.html", indexContent)
+ b.WithTemplatesAdded("index.fr.html", indexContent)
+
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/en/index.html", `
+Hello1: Hello
+Hello2: Hello
+`)
+
+ b.AssertFileContent("public/fr/index.html", `
+Hello1: Bonjour
+Hello2: Bonjour
+`)
+
}
--- a/tpl/template.go
+++ b/tpl/template.go
@@ -50,8 +50,6 @@
AddLateTemplate(name, tpl string) error
LoadTemplates(prefix string) error
- NewTextTemplate() TemplateParseFinder
-
MarkReady() error
RebuildClone()
}
--- a/tpl/tplimpl/template.go
+++ b/tpl/tplimpl/template.go
@@ -98,8 +98,6 @@
text *textTemplates
html *htmlTemplates
- extTextTemplates []*textTemplate
-
amberFuncMap template.FuncMap
errors []*templateErr
@@ -153,15 +151,7 @@
}
}
-// NewTextTemplate provides a text template parser that has all the Hugo
-// template funcs etc. built-in.
-func (t *templateHandler) NewTextTemplate() tpl.TemplateParseFinder {
- t.mu.Lock()
- defer t.mu.Unlock()
-
- tt := &textTemplate{t: texttemplate.New("")}
- t.extTextTemplates = append(t.extTextTemplates, tt)
-
+func (t *templateHandler) wrapTextTemplate(tt *textTemplate) tpl.TemplateParseFinder {
return struct {
tpl.TemplateParser
tpl.TemplateLookup
@@ -283,8 +273,11 @@
shortcodes: make(map[string]*shortcodeTemplates),
templateInfo: t.templateInfo,
html: &htmlTemplates{t: template.Must(t.html.t.Clone()), overlays: make(map[string]*template.Template), templatesCommon: t.html.templatesCommon},
- text: &textTemplates{textTemplate: &textTemplate{t: texttemplate.Must(t.text.t.Clone())}, overlays: make(map[string]*texttemplate.Template), templatesCommon: t.text.templatesCommon},
- errors: make([]*templateErr, 0),
+ text: &textTemplates{
+ textTemplate: &textTemplate{t: texttemplate.Must(t.text.t.Clone())},
+ standalone: &textTemplate{t: texttemplate.New("")},
+ overlays: make(map[string]*texttemplate.Template), templatesCommon: t.text.templatesCommon},
+ errors: make([]*templateErr, 0),
}
for k, v := range t.shortcodes {
@@ -302,6 +295,7 @@
}
d.Tmpl = c
+ d.TextTmpl = c.wrapTextTemplate(c.text.standalone)
c.initFuncs()
@@ -339,6 +333,7 @@
}
textT := &textTemplates{
textTemplate: &textTemplate{t: texttemplate.New("")},
+ standalone: &textTemplate{t: texttemplate.New("")},
overlays: make(map[string]*texttemplate.Template),
templatesCommon: common,
}
@@ -431,6 +426,7 @@
type textTemplates struct {
*templatesCommon
*textTemplate
+ standalone *textTemplate
clone *texttemplate.Template
cloneClone *texttemplate.Template
@@ -468,6 +464,7 @@
func (t *templateHandler) setFuncs(funcMap map[string]interface{}) {
t.html.setFuncs(funcMap)
t.text.setFuncs(funcMap)
+ t.setFuncMapInTemplate(t.text.standalone.t, funcMap)
}
// SetFuncs replaces the funcs in the func maps with new definitions.
@@ -779,10 +776,6 @@
for _, variant := range v.variants {
t.setFuncMapInTemplate(variant.templ, funcMap)
}
- }
-
- for _, extText := range t.extTextTemplates {
- extText.t.Funcs(funcMap)
}
// Amber is HTML only.
--- a/tpl/tplimpl/templateProvider.go
+++ b/tpl/tplimpl/templateProvider.go
@@ -26,12 +26,10 @@
// Update updates the Hugo Template System in the provided Deps
// with all the additional features, templates & functions.
func (*TemplateProvider) Update(deps *deps.Deps) error {
-
newTmpl := newTemplateAdapter(deps)
deps.Tmpl = newTmpl
+ deps.TextTmpl = newTmpl.wrapTextTemplate(newTmpl.text.standalone)
- deps.TextTmpl = newTmpl.NewTextTemplate()
-
newTmpl.initFuncs()
if err := newTmpl.loadEmbedded(); err != nil {
@@ -55,8 +53,6 @@
t := d.Tmpl.(*templateHandler)
clone := t.clone(d)
-
- d.Tmpl = clone
return clone.MarkReady()