Added implementation for a tpl function that evaluates a string as a template.

Implementation for #1978
reviewable/pr2350/r1
Lukas Eichler 8 years ago
parent 045bf78f80
commit d01f7978d2

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

@ -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)
}
}

Loading…
Cancel
Save