diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index ab813968f..f7fcf632d 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -64,7 +64,9 @@ func New() *Engine { // - "include": This is late-bound in Engine.Render(). The version // included in the FuncMap is a placeholder. // - "required": This is late-bound in Engine.Render(). The version -// included in thhe FuncMap is a placeholder. +// included in the FuncMap is a placeholder. +// - "tpl": This is late-bound in Engine.Render(). The version +// included in the FuncMap is a placeholder. func FuncMap() template.FuncMap { f := sprig.TxtFuncMap() delete(f, "env") @@ -83,6 +85,7 @@ func FuncMap() template.FuncMap { // integrity of the linter. "include": func(string, interface{}) string { return "not implemented" }, "required": func(string, interface{}) interface{} { return "not implemented" }, + "tpl": func(string, interface{}) interface{} { return "not implemented" }, } for k, v := range extra { @@ -158,6 +161,23 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap { return val, nil } + // Add the 'tpl' function here + funcMap["tpl"] = func(tpl string, vals chartutil.Values) string { + r := renderable{ + tpl: tpl, + vals: vals, + } + + templates := map[string]renderable{} + templates["template"] = r + + result, err := e.render(templates) + if err != nil { + return fmt.Errorf("Error during tpl function execution for %q", tpl).Error() + } + return result["template"] + } + return funcMap } diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index f821b2de5..461d8a8d5 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -49,7 +49,7 @@ func TestFuncMap(t *testing.T) { } // Test for Engine-specific template functions. - expect := []string{"include", "required", "toYaml", "fromYaml", "toToml", "toJson", "fromJson"} + expect := []string{"include", "required", "tpl", "toYaml", "fromYaml", "toToml", "toJson", "fromJson"} for _, f := range expect { if _, ok := fns[f]; !ok { t.Errorf("Expected add-on function %q", f) @@ -445,4 +445,33 @@ func TestAlterFuncMap(t *testing.T) { t.Errorf("Expected %q, got %q (%v)", expectNum, gotNum, outReq) } + tplChart := &chart.Chart{ + Metadata: &chart.Metadata{Name: "TplFunction"}, + Templates: []*chart.Template{ + {Name: "templates/base", Data: []byte(`Evaluate tpl {{tpl "Value: {{ .Values.value}}" .}}`)}, + }, + Values: &chart.Config{Raw: ``}, + Dependencies: []*chart.Chart{}, + } + + tplValues := chartutil.Values{ + "Values": chartutil.Values{ + "value": "myvalue", + }, + "Chart": tplChart.Metadata, + "Release": chartutil.Values{ + "Name": "TestRelease", + }, + } + + outTpl, err := New().Render(tplChart, tplValues) + if err != nil { + t.Fatal(err) + } + + expectTplStr := "Evaluate tpl Value: myvalue" + if gotStrTpl := outTpl["TplFunction/templates/base"]; gotStrTpl != expectTplStr { + t.Errorf("Expected %q, got %q (%v)", expectTplStr, gotStrTpl, outTpl) + } + }