summaryrefslogtreecommitdiffstats
path: root/tpl/tplimpl/template_funcs.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-01-04 18:24:36 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-05-16 18:01:29 +0200
commit241b21b0fd34d91fccb2ce69874110dceae6f926 (patch)
treed4e0118eac7e9c42f065815447a70805f8d6ad3e /tpl/tplimpl/template_funcs.go
parent6aededf6b42011c3039f5f66487a89a8dd65e0e7 (diff)
Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code. Also, * Lower case the default output format names; this is in line with the custom ones (map keys) and how it's treated all the places. This avoids doing `stringds.EqualFold` everywhere. Closes #10896 Closes #10620
Diffstat (limited to 'tpl/tplimpl/template_funcs.go')
-rw-r--r--tpl/tplimpl/template_funcs.go21
1 files changed, 17 insertions, 4 deletions
diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go
index 1979fa1c9..97d1b40dd 100644
--- a/tpl/tplimpl/template_funcs.go
+++ b/tpl/tplimpl/template_funcs.go
@@ -71,8 +71,10 @@ var (
)
type templateExecHelper struct {
- running bool // whether we're in server mode.
- funcs map[string]reflect.Value
+ running bool // whether we're in server mode.
+ site reflect.Value
+ siteParams reflect.Value
+ funcs map[string]reflect.Value
}
func (t *templateExecHelper) GetFunc(ctx context.Context, tmpl texttemplate.Preparer, name string) (fn reflect.Value, firstArg reflect.Value, found bool) {
@@ -111,6 +113,8 @@ func (t *templateExecHelper) GetMapValue(ctx context.Context, tmpl texttemplate.
return v, v.IsValid()
}
+var typeParams = reflect.TypeOf(maps.Params{})
+
func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value) {
if t.running {
switch name {
@@ -123,6 +127,13 @@ func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Pr
}
}
+ if strings.EqualFold(name, "mainsections") && receiver.Type() == typeParams && receiver.Pointer() == t.siteParams.Pointer() {
+ // MOved to site.MainSections in Hugo 0.112.0.
+ receiver = t.site
+ name = "MainSections"
+
+ }
+
fn := hreflect.GetMethodByName(receiver, name)
if !fn.IsValid() {
return zero, zero
@@ -167,8 +178,10 @@ func newTemplateExecuter(d *deps.Deps) (texttemplate.Executer, map[string]reflec
}
exeHelper := &templateExecHelper{
- running: d.Running,
- funcs: funcsv,
+ running: d.Conf.Running(),
+ funcs: funcsv,
+ site: reflect.ValueOf(d.Site),
+ siteParams: reflect.ValueOf(d.Site.Params()),
}
return texttemplate.NewExecuter(