From 7ad2cfdfbcf4acd74acba686f8edffbe2e5110bf Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Fri, 25 Jul 2014 17:32:17 +0100 Subject: [PATCH] Add 'tmpl', a 'template' for non-string literal names. Change-Id: I6a03a5c5d20029cf414562efa7745ed6c53b2731 --- templates/templates.go | 10 +++++++++- templates/templates_test.go | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/templates/templates.go b/templates/templates.go index 167554c15..d04ad6aeb 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -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) } diff --git a/templates/templates_test.go b/templates/templates_test.go index 23e9babb3..5aff1d860 100644 --- a/templates/templates_test.go +++ b/templates/templates_test.go @@ -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)