Add 'tmpl', a 'template' for non-string literal names.

Change-Id: I6a03a5c5d20029cf414562efa7745ed6c53b2731
This commit is contained in:
Brian Brazil 2014-07-25 17:32:17 +01:00 committed by Bjoern Rabenstein
parent f525ca5d9e
commit 144d5bb9fd
2 changed files with 15 additions and 1 deletions

View file

@ -259,7 +259,15 @@ func (te templateExpander) ExpandHTML(templateFiles []string) (result string, re
}()
var buffer bytes.Buffer
tmpl, err := html_template.New(te.name).Funcs(html_template.FuncMap(te.funcMap)).Parse(te.text)
tmpl := html_template.New(te.name).Funcs(html_template.FuncMap(te.funcMap))
tmpl.Funcs(html_template.FuncMap{
"tmpl": func(name string, data interface{}) (html_template.HTML, error) {
var buffer bytes.Buffer
err := tmpl.ExecuteTemplate(&buffer, name, data)
return html_template.HTML(buffer.String()), err
},
})
tmpl, err := tmpl.Parse(te.text)
if err != nil {
return "", fmt.Errorf("Error parsing template %v: %v", te.name, err)
}

View file

@ -147,6 +147,12 @@ func TestTemplateExpansion(t *testing.T) {
text: "{{ tableLink \"up\" }}",
output: "/graph#%5B%7B%22expr%22%3A%22up%22%2C%22tab%22%3A1%7D%5D",
},
{
// tmpl.
text: "{{ define \"a\" }}x{{ end }}{{ $name := \"a\"}}{{ tmpl $name . }}",
output: "x",
html: true,
},
}
time := clientmodel.Timestamp(0)