"container/vector"
"fmt"
"io"
+ "io/ioutil"
"os"
"reflect"
"runtime"
return
}
+// ParseFile is a wrapper function that creates a Template with default
+// parameters (such as {} for // metacharacters). The filename identfies
+// a file containing the template text, while the formatter map fmap, which
+// may be nil, defines auxiliary functions for formatting variables.
+// The template is returned. If any errors occur, err will be non-nil.
+func ParseFile(filename string, fmap FormatterMap) (t *Template, err os.Error) {
+ b, err := ioutil.ReadFile(filename)
+ if err != nil {
+ return nil, err
+ }
+ return Parse(string(b), fmap)
+}
+
// MustParse is like Parse but panics if the template cannot be parsed.
func MustParse(s string, fmap FormatterMap) *Template {
t, err := Parse(s, fmap)
}
return t
}
+
+// MustParseFile is like ParseFile but panics if the file cannot be read
+// or the template cannot be parsed.
+func MustParseFile(filename string, fmap FormatterMap) *Template {
+ b, err := ioutil.ReadFile(filename)
+ if err != nil {
+ panic("template parse error: ", err.String())
+ }
+ return MustParse(string(b), fmap)
+}
"container/vector"
"fmt"
"io"
+ "io/ioutil"
"json"
+ "os"
"testing"
)
}
func TestAll(t *testing.T) {
+ // Parse
+ 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)
+ return ParseFile("_test/test.tmpl", formatters)
+ })
+}
+
+func testAll(t *testing.T, parseFunc func(*Test) (*Template, os.Error)) {
s := new(S)
// initialized by hand for clarity.
s.header = "Header"
var buf bytes.Buffer
for _, test := range tests {
buf.Reset()
- tmpl, err := Parse(test.in, formatters)
+ tmpl, err := parseFunc(test)
if err != nil {
t.Error("unexpected parse error:", err)
continue