diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-02-25 09:24:59 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-02-25 19:53:18 +0100 |
commit | ce524d0b5ebaef05d29fa368465f31358f26dcda (patch) | |
tree | e5df54a5deeefacbff4916d3619f85c2cb341b01 /tpl/internal | |
parent | 2662faf61ff0240be1ee0d6c496b6b4a6ed55fb4 (diff) |
Add a page template func
Fixes #9339
Diffstat (limited to 'tpl/internal')
-rw-r--r-- | tpl/internal/go_templates/texttemplate/hugo_template.go | 36 | ||||
-rw-r--r-- | tpl/internal/templatefuncsRegistry.go | 11 |
2 files changed, 13 insertions, 34 deletions
diff --git a/tpl/internal/go_templates/texttemplate/hugo_template.go b/tpl/internal/go_templates/texttemplate/hugo_template.go index 96f526005..23bc18e42 100644 --- a/tpl/internal/go_templates/texttemplate/hugo_template.go +++ b/tpl/internal/go_templates/texttemplate/hugo_template.go @@ -60,29 +60,29 @@ func NewExecuter(helper ExecHelper) Executer { } type ( - dataContextKeyType string + pageContextKeyType string hasLockContextKeyType string stackContextKeyType string ) const ( - // The data object passed to Execute or ExecuteWithContext gets stored with this key if not already set. - DataContextKey = dataContextKeyType("data") + // The data page passed to ExecuteWithContext gets stored with this key. + PageContextKey = pageContextKeyType("page") // Used in partialCached to signal to nested templates that a lock is already taken. HasLockContextKey = hasLockContextKeyType("hasLock") ) // Note: The context is currently not fully implemeted in Hugo. This is a work in progress. func (t *executer) ExecuteWithContext(ctx context.Context, p Preparer, wr io.Writer, data any) error { + if ctx == nil { + panic("nil context") + } + tmpl, err := p.Prepare() if err != nil { return err } - if v := ctx.Value(DataContextKey); v == nil { - ctx = context.WithValue(ctx, DataContextKey, data) - } - value, ok := data.(reflect.Value) if !ok { value = reflect.ValueOf(data) @@ -102,28 +102,6 @@ func (t *executer) ExecuteWithContext(ctx context.Context, p Preparer, wr io.Wri return tmpl.executeWithState(state, value) } -func (t *executer) Execute(p Preparer, wr io.Writer, data any) error { - tmpl, err := p.Prepare() - if err != nil { - return err - } - - value, ok := data.(reflect.Value) - if !ok { - value = reflect.ValueOf(data) - } - - state := &state{ - helper: t.helper, - prep: p, - tmpl: tmpl, - wr: wr, - vars: []variable{{"$", value}}, - } - - return tmpl.executeWithState(state, value) -} - // Prepare returns a template ready for execution. func (t *Template) Prepare() (*Template, error) { return t, nil diff --git a/tpl/internal/templatefuncsRegistry.go b/tpl/internal/templatefuncsRegistry.go index 84e0e25fa..363f6d82f 100644 --- a/tpl/internal/templatefuncsRegistry.go +++ b/tpl/internal/templatefuncsRegistry.go @@ -17,6 +17,7 @@ package internal import ( "bytes" + "context" "encoding/json" "fmt" "go/doc" @@ -49,7 +50,7 @@ type TemplateFuncsNamespace struct { Name string // This is the method receiver. - Context func(v ...any) (any, error) + Context func(ctx context.Context, v ...any) (any, error) // Additional info, aliases and examples, per method name. MethodMappings map[string]TemplateFuncMethodMapping @@ -172,7 +173,7 @@ func (namespaces TemplateFuncsNamespaces) MarshalJSON() ([]byte, error) { if i != 0 { buf.WriteString(",") } - b, err := ns.toJSON() + b, err := ns.toJSON(context.TODO()) if err != nil { return nil, err } @@ -188,7 +189,7 @@ var ignoreFuncs = map[string]bool{ "Reset": true, } -func (t *TemplateFuncsNamespace) toJSON() ([]byte, error) { +func (t *TemplateFuncsNamespace) toJSON(ctx context.Context) ([]byte, error) { var buf bytes.Buffer godoc := getGetTplPackagesGoDoc()[t.Name] @@ -197,11 +198,11 @@ func (t *TemplateFuncsNamespace) toJSON() ([]byte, error) { buf.WriteString(fmt.Sprintf(`%q: {`, t.Name)) - ctx, err := t.Context() + tctx, err := t.Context(ctx) if err != nil { return nil, err } - ctxType := reflect.TypeOf(ctx) + ctxType := reflect.TypeOf(tctx) for i := 0; i < ctxType.NumMethod(); i++ { method := ctxType.Method(i) if ignoreFuncs[method.Name] { |