OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package template | 5 package template |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "io" | 9 "io" |
10 "reflect" | 10 "reflect" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 97 |
98 // Execute applies a parsed template to the specified data object, | 98 // Execute applies a parsed template to the specified data object, |
99 // and writes the output to wr. | 99 // and writes the output to wr. |
100 func (t *Template) Execute(wr io.Writer, data interface{}) (err error) { | 100 func (t *Template) Execute(wr io.Writer, data interface{}) (err error) { |
101 defer errRecover(&err) | 101 defer errRecover(&err) |
102 value := reflect.ValueOf(data) | 102 value := reflect.ValueOf(data) |
103 state := &state{ | 103 state := &state{ |
104 tmpl: t, | 104 tmpl: t, |
105 wr: wr, | 105 wr: wr, |
106 line: 1, | 106 line: 1, |
107 » » vars: []variable{{"$", value}}, | 107 » » vars: {{"$", value}}, |
108 } | 108 } |
109 if t.Tree == nil || t.Root == nil { | 109 if t.Tree == nil || t.Root == nil { |
110 state.errorf("must be parsed before execution") | 110 state.errorf("must be parsed before execution") |
111 } | 111 } |
112 state.walk(value, t.Root) | 112 state.walk(value, t.Root) |
113 return | 113 return |
114 } | 114 } |
115 | 115 |
116 // Walk functions step through the major pieces of the template structure, | 116 // Walk functions step through the major pieces of the template structure, |
117 // generating output as they go. | 117 // generating output as they go. |
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 v = v.Addr() | 659 v = v.Addr() |
660 } else { | 660 } else { |
661 switch v.Kind() { | 661 switch v.Kind() { |
662 case reflect.Chan, reflect.Func: | 662 case reflect.Chan, reflect.Func: |
663 s.errorf("can't print %s of type %s", n, v.Type(
)) | 663 s.errorf("can't print %s of type %s", n, v.Type(
)) |
664 } | 664 } |
665 } | 665 } |
666 } | 666 } |
667 fmt.Fprint(s.wr, v.Interface()) | 667 fmt.Fprint(s.wr, v.Interface()) |
668 } | 668 } |
OLD | NEW |