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>2016-10-24 23:29:48 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-11-22 19:33:52 +0300
commite1da7cb3204c6d232f4a1bdbf89ce75d8459a8b8 (patch)
treedbba1de243b63069da3c3f7a88a6532c0b77b773 /tpl/template.go
parenta5b3b076570eb87b856be964f36c3ef19fe1d7e2 (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.go29
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)
}
}