Browse Source

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

Change-Id: I6a03a5c5d20029cf414562efa7745ed6c53b2731
changes/46/346/1
Brian Brazil 10 years ago
parent
commit
7ad2cfdfbc
  1. 10
      templates/templates.go
  2. 6
      templates/templates_test.go

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

6
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)

Loading…
Cancel
Save