diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-11-22 09:04:38 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-22 09:04:38 +0100 |
commit | 239c75c7f866e8970b794a9e7bac73de46052241 (patch) | |
tree | a198e4850fb683929be66c47d2a75e86d2fcfb96 /tpl/template.go | |
parent | 1eb3c4a8e717f47906dcce01af239cad02eed7eb (diff) |
Fix case issues with Params
There are currently several Params and case related issues floating around in Hugo.
This is very confusing for users and one of the most common support questions on the forum.
And while there have been done some great leg work in Viper etc., this is of limited value since this and similar doesn't work:
`Params.myCamelCasedParam`
Hugo has control over all the template method invocations, and can take care of all the lower-casing of the map lookup keys.
But that doesn't help with direct template lookups of type `Site.Params.TWITTER_CONFIG.USER_ID`.
This commit solves that by doing some carefully crafted modifications of the templates' AST -- lowercasing the params keys.
This is low-level work, but it's not like the template API wil change -- and this is important enough to defend such "bit fiddling".
Tests are added for all the template engines: Go templates, Ace and Amber.
Fixes #2615
Fixes #1129
Fixes #2590
Diffstat (limited to 'tpl/template.go')
-rw-r--r-- | tpl/template.go | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/tpl/template.go b/tpl/template.go index 2d8ed2943..275ec6b8f 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -213,11 +213,16 @@ func (t *GoHTMLTemplate) AddInternalShortcode(name, content string) error { func (t *GoHTMLTemplate) AddTemplate(name, tpl string) error { t.checkState() - _, err := t.New(name).Parse(tpl) + templ, err := t.New(name).Parse(tpl) if err != nil { t.errors = append(t.errors, &templateErr{name: name, err: err}) + return err } - return err + if err := applyTemplateTransformers(templ); err != nil { + return err + } + + return nil } func (t *GoHTMLTemplate) AddTemplateFileWithMaster(name, overlayFilename, masterFilename string) error { @@ -264,7 +269,11 @@ func (t *GoHTMLTemplate) AddTemplateFileWithMaster(name, overlayFilename, master // The extra lookup is a workaround, see // * https://github.com/golang/go/issues/16101 // * https://github.com/spf13/hugo/issues/2549 - t.overlays[name] = overlayTpl.Lookup(overlayTpl.Name()) + overlayTpl = overlayTpl.Lookup(overlayTpl.Name()) + if err := applyTemplateTransformers(overlayTpl); err != nil { + return err + } + t.overlays[name] = overlayTpl } return err @@ -291,11 +300,12 @@ func (t *GoHTMLTemplate) AddAceTemplate(name, basePath, innerPath string, baseCo t.errors = append(t.errors, &templateErr{name: name, err: err}) return err } - _, err = ace.CompileResultWithTemplate(t.New(name), parsed, nil) + templ, err := ace.CompileResultWithTemplate(t.New(name), parsed, nil) if err != nil { t.errors = append(t.errors, &templateErr{name: name, err: err}) + return err } - return err + return applyTemplateTransformers(templ) } func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) error { @@ -317,9 +327,12 @@ func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) er return err } - if _, err := compiler.CompileWithTemplate(t.New(templateName)); err != nil { + templ, err := compiler.CompileWithTemplate(t.New(templateName)) + if err != nil { return err } + + return applyTemplateTransformers(templ) case ".ace": var innerContent, baseContent []byte innerContent, err := afero.ReadFile(hugofs.Source(), path) @@ -353,8 +366,6 @@ func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) er return t.AddTemplate(name, string(b)) } - return nil - } func (t *GoHTMLTemplate) GenerateTemplateNameFrom(base, path string) string { @@ -467,7 +478,7 @@ func (t *GoHTMLTemplate) loadTemplates(absPath string, prefix string) { } if err := t.AddTemplateFile(tplName, baseTemplatePath, path); err != nil { - jww.ERROR.Printf("Failed to add template %s: %s", tplName, err) + jww.ERROR.Printf("Failed to add template %s in path %s: %s", tplName, path, err) } } |