diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-09-13 13:13:40 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-09-13 13:13:40 +0200 |
commit | 5ff4c17907d5b19510a62e08fd8d3b11e62b431d (patch) | |
tree | c0650497e988f47be9c6f2324fa692a52dea82e1 /src/pkg/template/parse.go | |
parent | 80f18fc933cf3f3e829c5455a1023d69f7b86e52 (diff) | |
download | golang-5ff4c17907d5b19510a62e08fd8d3b11e62b431d.tar.gz |
Imported Upstream version 60upstream/60
Diffstat (limited to 'src/pkg/template/parse.go')
-rw-r--r-- | src/pkg/template/parse.go | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/pkg/template/parse.go b/src/pkg/template/parse.go new file mode 100644 index 000000000..b089c599a --- /dev/null +++ b/src/pkg/template/parse.go @@ -0,0 +1,85 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package template + +import ( + "os" + "reflect" + "template/parse" +) + +// Template is the representation of a parsed template. +type Template struct { + name string + *parse.Tree + // We use two maps, one for parsing and one for execution. + // This separation makes the API cleaner since it doesn't + // expose reflection to the client. + parseFuncs FuncMap + execFuncs map[string]reflect.Value + set *Set // can be nil. +} + +// Name returns the name of the template. +func (t *Template) Name() string { + return t.name +} + +// Parsing. + +// New allocates a new template with the given name. +func New(name string) *Template { + return &Template{ + name: name, + parseFuncs: make(FuncMap), + execFuncs: make(map[string]reflect.Value), + } +} + +// Funcs adds the elements of the argument map to the template's function +// map. It panics if a value in the map is not a function with appropriate +// return type. +// The return value is the template, so calls can be chained. +func (t *Template) Funcs(funcMap FuncMap) *Template { + addValueFuncs(t.execFuncs, funcMap) + addFuncs(t.parseFuncs, funcMap) + return t +} + +// Parse parses the template definition string to construct an internal +// representation of the template for execution. +func (t *Template) Parse(s string) (tmpl *Template, err os.Error) { + t.Tree, err = parse.New(t.name).Parse(s, t.parseFuncs, builtins) + if err != nil { + return nil, err + } + return t, nil +} + +// ParseInSet parses the template definition string to construct an internal +// representation of the template for execution. It also adds the template +// to the set. +// Function bindings are checked against those in the set. +func (t *Template) ParseInSet(s string, set *Set) (tmpl *Template, err os.Error) { + var setFuncs FuncMap + if set != nil { + setFuncs = set.parseFuncs + } + t.Tree, err = parse.New(t.name).Parse(s, t.parseFuncs, setFuncs, builtins) + if err != nil { + return nil, err + } + t.addToSet(set) + return t, nil +} + +// addToSet adds the template to the set, verifying it's not being double-assigned. +func (t *Template) addToSet(set *Set) { + if set == nil || t.set == set { + return + } + // If double-assigned, Add will panic and we will turn that into an error. + set.Add(t) +} |