1
0
mirror of https://github.com/golang/go synced 2024-11-24 23:57:57 -07:00

template: added ParseFile method for template.Template

Fixes #971.

Parse/ParseFile methods of Template now match template.Parse and .ParseFile methods.
Also made tests being run on Parse and ParseFile be run on Template.ParseFile as well.

R=r
CC=golang-dev
https://golang.org/cl/1741059
This commit is contained in:
Scott Lawrence 2010-08-11 14:04:03 +10:00 committed by Rob Pike
parent a9a62eef6b
commit bca3151042
2 changed files with 25 additions and 1 deletions

View File

@ -888,6 +888,16 @@ func (t *Template) Parse(s string) (err os.Error) {
return nil
}
// ParseFile is like Parse but reads the template definition from the
// named file.
func (t *Template) ParseFile(filename string) (err os.Error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return t.Parse(string(b))
}
// Execute applies a parsed template to the specified data object,
// generating output to wr.
func (t *Template) Execute(data interface{}, wr io.Writer) (err os.Error) {

View File

@ -398,9 +398,23 @@ func TestAll(t *testing.T) {
testAll(t, func(test *Test) (*Template, os.Error) { return Parse(test.in, formatters) })
// ParseFile
testAll(t, func(test *Test) (*Template, os.Error) {
ioutil.WriteFile("_test/test.tmpl", []byte(test.in), 0600)
err := ioutil.WriteFile("_test/test.tmpl", []byte(test.in), 0600)
if err != nil {
t.Error("unexpected write error:", err)
return nil, err
}
return ParseFile("_test/test.tmpl", formatters)
})
// tmpl.ParseFile
testAll(t, func(test *Test) (*Template, os.Error) {
err := ioutil.WriteFile("_test/test.tmpl", []byte(test.in), 0600)
if err != nil {
t.Error("unexpected write error:", err)
return nil, err
}
tmpl := New(formatters)
return tmpl, tmpl.ParseFile("_test/test.tmpl")
})
}
func testAll(t *testing.T, parseFunc func(*Test) (*Template, os.Error)) {