Remove the 'reference templates' concept

As we're using `t.Clone()` we already get the right 'references'.

Signed-off-by: Graham Reed <greed@7deadly.org>
pull/11351/head
Graham Reed 2 years ago
parent db4f330122
commit e2a7c7998a

@ -123,7 +123,7 @@ func includeFun(t *template.Template, includedNames map[string]int) func(string,
} }
// initFunMap creates the Engine's FuncMap and adds context-specific functions. // initFunMap creates the Engine's FuncMap and adds context-specific functions.
func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]renderable) { func (e Engine) initFunMap(t *template.Template) {
funcMap := funcMap() funcMap := funcMap()
includedNames := make(map[string]int) includedNames := make(map[string]int)
@ -154,7 +154,7 @@ func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]render
return "", errors.Wrapf(err, "error during tpl function execution for %q", tpl) return "", errors.Wrapf(err, "error during tpl function execution for %q", tpl)
} }
// See comment in renderWithReferences explaining the <no value> hack. // See comment in render explaining the <no value> hack.
return strings.ReplaceAll(buf.String(), "<no value>", ""), nil return strings.ReplaceAll(buf.String(), "<no value>", ""), nil
} }
@ -200,13 +200,7 @@ func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]render
} }
// render takes a map of templates/values and renders them. // render takes a map of templates/values and renders them.
func (e Engine) render(tpls map[string]renderable) (map[string]string, error) { func (e Engine) render(tpls map[string]renderable) (rendered map[string]string, err error) {
return e.renderWithReferences(tpls, tpls)
}
// renderWithReferences takes a map of templates/values to render, and a map of
// templates which can be referenced within them.
func (e Engine) renderWithReferences(tpls, referenceTpls map[string]renderable) (rendered map[string]string, err error) {
// Basically, what we do here is start with an empty parent template and then // Basically, what we do here is start with an empty parent template and then
// build up a list of templates -- one for each file. Once all of the templates // build up a list of templates -- one for each file. Once all of the templates
// have been parsed, we loop through again and execute every template. // have been parsed, we loop through again and execute every template.
@ -228,12 +222,11 @@ func (e Engine) renderWithReferences(tpls, referenceTpls map[string]renderable)
t.Option("missingkey=zero") t.Option("missingkey=zero")
} }
e.initFunMap(t, referenceTpls) e.initFunMap(t)
// We want to parse the templates in a predictable order. The order favors // We want to parse the templates in a predictable order. The order favors
// higher-level (in file system) templates over deeply nested templates. // higher-level (in file system) templates over deeply nested templates.
keys := sortTemplates(tpls) keys := sortTemplates(tpls)
referenceKeys := sortTemplates(referenceTpls)
for _, filename := range keys { for _, filename := range keys {
r := tpls[filename] r := tpls[filename]
@ -242,17 +235,6 @@ func (e Engine) renderWithReferences(tpls, referenceTpls map[string]renderable)
} }
} }
// Adding the reference templates to the template context
// so they can be referenced in the tpl function
for _, filename := range referenceKeys {
if t.Lookup(filename) == nil {
r := referenceTpls[filename]
if _, err := t.New(filename).Parse(r.tpl); err != nil {
return map[string]string{}, cleanupParseError(filename, err)
}
}
}
rendered = make(map[string]string, len(keys)) rendered = make(map[string]string, len(keys))
for _, filename := range keys { for _, filename := range keys {
// Don't render partials. We don't care out the direct output of partials. // Don't render partials. We don't care out the direct output of partials.

Loading…
Cancel
Save