mirror of
https://github.com/golang/go
synced 2024-11-25 02:47:58 -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:
parent
a9a62eef6b
commit
bca3151042
@ -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) {
|
||||
|
@ -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)) {
|
||||
|
Loading…
Reference in New Issue
Block a user