diff --git a/pkg/engine/debug.test b/pkg/engine/debug.test new file mode 100755 index 000000000..873fa4cae Binary files /dev/null and b/pkg/engine/debug.test differ diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index 3c80e39fa..a987fb3b8 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -171,13 +171,13 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap { } templates := map[string]renderable{} - templates["template"] = r + templates["aaa_template"] = r result, err := e.render(templates) if err != nil { return "", fmt.Errorf("Error during tpl function execution for %q: %s", tpl, err.Error()) } - return result["template"], nil + return result["aaa_template"], nil } return funcMap @@ -201,15 +201,10 @@ func (e *Engine) render(tpls map[string]renderable) (map[string]string, error) { t.Option("missingkey=zero") } - // Adding templates from the engine context but do not overwrite - for k, v := range e.CurrentTemplates { - if _, exists := tpls[k]; exists { - tpls[k] = v - } - } funcMap := e.alterFuncMap(t) files := []string{} + for fname, r := range tpls { t = t.New(fname).Funcs(funcMap) if _, err := t.Parse(r.tpl); err != nil { @@ -218,6 +213,17 @@ func (e *Engine) render(tpls map[string]renderable) (map[string]string, error) { files = append(files, fname) } + // Adding the engine's currentTemplates to the template context + // so they can be referenced in the tpl function + for fname, r := range e.CurrentTemplates { + if t.Lookup(fname) == nil { + t = t.New(fname).Funcs(funcMap) + if _, err := t.Parse(r.tpl); err != nil { + return map[string]string{}, fmt.Errorf("parse error in %q: %s", fname, err) + } + } + } + rendered := make(map[string]string, len(files)) var buf bytes.Buffer for _, file := range files { diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index 0c65cdbf9..fcf9c1c64 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -503,4 +503,33 @@ func TestAlterFuncMap(t *testing.T) { t.Errorf("Expected %q, got %q (%v)", expectTplStrWithFunction, gotStrTplWithFunction, outTplWithFunction) } + tplChartWithInclude := &chart.Chart{ + Metadata: &chart.Metadata{Name: "TplFunction"}, + Templates: []*chart.Template{ + {Name: "templates/base", Data: []byte(`{{ tpl "{{include ` + "`" + `TplFunction/templates/_partial` + "`" + ` . | quote }}" .}}`)}, + {Name: "templates/_partial", Data: []byte(`{{.Release.Name}}`)}, + }, + Values: &chart.Config{Raw: ``}, + Dependencies: []*chart.Chart{}, + } + tplValueWithInclude := chartutil.Values{ + "Values": chartutil.Values{ + "value": "myvalue", + }, + "Chart": tplChartWithInclude.Metadata, + "Release": chartutil.Values{ + "Name": "TestRelease", + }, + } + + outTplWithInclude, err := New().Render(tplChartWithInclude, tplValueWithInclude) + if err != nil { + t.Fatal(err) + } + + expectedTplStrWithInclude := "\"TestRelease\"" + if gotStrTplWithInclude := outTplWithInclude["TplFunction/templates/base"]; gotStrTplWithInclude != expectedTplStrWithInclude { + t.Errorf("Expected %q, got %q (%v)", expectedTplStrWithInclude, gotStrTplWithInclude, outTplWithInclude) + } + }