Skip to content

Commit

Permalink
Remove the 'reference templates' concept
Browse files Browse the repository at this point in the history
As we're using `t.Clone()` we already get the right 'references'.

Signed-off-by: Graham Reed <greed@7deadly.org>
  • Loading branch information
greed42 committed Oct 20, 2022
1 parent db4f330 commit e2a7c79
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions pkg/engine/engine.go
Expand Up @@ -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.
func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]renderable) {
func (e Engine) initFunMap(t *template.Template) {
funcMap := funcMap()
includedNames := make(map[string]int)

Expand Down Expand Up @@ -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)
}

// 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
}

Expand Down Expand Up @@ -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.
func (e Engine) render(tpls map[string]renderable) (map[string]string, 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) {
func (e Engine) render(tpls map[string]renderable) (rendered map[string]string, err error) {
// 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
// have been parsed, we loop through again and execute every template.
Expand All @@ -228,12 +222,11 @@ func (e Engine) renderWithReferences(tpls, referenceTpls map[string]renderable)
t.Option("missingkey=zero")
}

e.initFunMap(t, referenceTpls)
e.initFunMap(t)

// We want to parse the templates in a predictable order. The order favors
// higher-level (in file system) templates over deeply nested templates.
keys := sortTemplates(tpls)
referenceKeys := sortTemplates(referenceTpls)

for _, filename := range keys {
r := tpls[filename]
Expand All @@ -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))
for _, filename := range keys {
// Don't render partials. We don't care out the direct output of partials.
Expand Down

0 comments on commit e2a7c79

Please sign in to comment.