diff options
author | Russ Cox <rsc@golang.org> | 2009-09-01 16:31:49 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-09-01 16:31:49 -0700 |
commit | 283f6a77ba5fed89a7ac29bf640d77644592a04e (patch) | |
tree | c93df9c32f01bfb387f409c9a55f3f8a9b958bc3 /src/pkg/template/template.go | |
parent | c3b5b2c21ed2813738ca0aa074320915edd70d85 (diff) | |
download | golang-283f6a77ba5fed89a7ac29bf640d77644592a04e.tar.gz |
template:
* do full lookup for {.section},
so that it is always allowed to replace
{Foo} with {.section Foo}{@}{.end}
* treat False as empty so that .section can
be used to test bools
R=r
DELTA=29 (21 added, 0 deleted, 8 changed)
OCL=34215
CL=34219
Diffstat (limited to 'src/pkg/template/template.go')
-rw-r--r-- | src/pkg/template/template.go | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/pkg/template/template.go b/src/pkg/template/template.go index 0df31755a..847e58f44 100644 --- a/src/pkg/template/template.go +++ b/src/pkg/template/template.go @@ -595,6 +595,8 @@ func empty(v reflect.Value) bool { return true } switch v := v.(type) { + case *reflect.BoolValue: + return v.Get() == false; case *reflect.StringValue: return v.Get() == ""; case *reflect.StructValue: @@ -608,13 +610,13 @@ func empty(v reflect.Value) bool { } // Look up a variable, up through the parent if necessary. -func (t *Template) varValue(v *variableElement, st *state) reflect.Value { - field := st.findVar(v.name); +func (t *Template) varValue(name string, st *state) reflect.Value { + field := st.findVar(name); if field == nil { if st.parent == nil { - t.execError(st, t.linenum, "name not found: %s", v.name) + t.execError(st, t.linenum, "name not found: %s", name) } - return t.varValue(v, st.parent); + return t.varValue(name, st.parent); } return field; } @@ -623,7 +625,7 @@ func (t *Template) varValue(v *variableElement, st *state) reflect.Value { // If it has a formatter attached ({var|formatter}) run that too. func (t *Template) writeVariable(v *variableElement, st *state) { formatter := v.formatter; - val := t.varValue(v, st).Interface(); + val := t.varValue(v.name, st).Interface(); // is it in user-supplied map? if t.fmap != nil { if fn, ok := t.fmap[v.formatter]; ok { @@ -673,7 +675,7 @@ func (t *Template) execute(start, end int, st *state) { // Execute a .section func (t *Template) executeSection(s *sectionElement, st *state) { // Find driver data for this section. It must be in the current struct. - field := st.findVar(s.field); + field := t.varValue(s.field, st); if field == nil { t.execError(st, s.linenum, ".section: cannot find field %s in %s", s.field, reflect.Indirect(st.data).Type()); } @@ -718,7 +720,7 @@ func iter(v reflect.Value) *reflect.ChanValue { // Execute a .repeated section func (t *Template) executeRepeated(r *repeatedElement, st *state) { // Find driver data for this section. It must be in the current struct. - field := st.findVar(r.field); + field := t.varValue(r.field, st); if field == nil { t.execError(st, r.linenum, ".repeated: cannot find field %s in %s", r.field, reflect.Indirect(st.data).Type()); } |