1
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
16
// Set holds a set of related templates that can refer to one another by name.
17
// A template may be a member of multiple sets.
19
tmpl map[string]*Template
20
funcs map[string]reflect.Value
23
// NewSet allocates a new, empty template set.
26
tmpl: make(map[string]*Template),
27
funcs: make(map[string]reflect.Value),
31
// Funcs adds to the set's function map the elements of the
32
// argument map. It panics if a value in the map is not a function
33
// with appropriate return type.
34
// The return value is the set, so calls can be chained.
35
func (s *Set) Funcs(funcMap FuncMap) *Set {
36
addFuncs(s.funcs, funcMap)
40
// Add adds the argument templates to the set. It panics if the call
41
// attempts to reuse a name defined in the template.
42
// The return value is the set, so calls can be chained.
43
func (s *Set) Add(templates ...*Template) *Set {
44
for _, t := range templates {
45
if _, ok := s.tmpl[t.name]; ok {
46
panic(fmt.Errorf("template: %q already defined in set", t.name))
53
// Template returns the template with the given name in the set,
54
// or nil if there is no such template.
55
func (s *Set) Template(name string) *Template {
59
// Execute looks for the named template in the set and then applies that
60
// template to the specified data object, writing the output to wr. Nested
61
// template invocations will be resolved from the set.
62
func (s *Set) Execute(name string, wr io.Writer, data interface{}) os.Error {
65
return fmt.Errorf("template: no template %q in set", name)
67
return tmpl.ExecuteInSet(wr, data, s)
70
// recover is the handler that turns panics into returns from the top
72
func (s *Set) recover(errp *os.Error) {
75
if _, ok := e.(runtime.Error); ok {
84
// Parse parses the file into a set of named templates.
85
func (s *Set) Parse(text string) (err os.Error) {
87
lex := lex("set", text)
88
const context = "define clause"
90
t := New("set") // name will be updated once we know it.
92
// Expect EOF or "{{ define name }}".
96
t.expect(itemLeftDelim, context)
97
t.expect(itemDefine, context)
98
name := t.expect(itemString, context)
99
t.name, err = strconv.Unquote(name.val)
103
t.expect(itemRightDelim, context)
104
end := t.parse(false)
106
t.errorf("unexpected EOF in %s", context)
108
if end.typ() != nodeEnd {
109
t.errorf("unexpected %s in %s", end, context)