diff options
| author | Rob Pike <r@golang.org> | 2012-08-24 12:37:23 -0700 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2012-08-24 12:37:23 -0700 |
| commit | cc842c738ea9a64570e306cbab37c3e3cf9a35dd (patch) | |
| tree | d551a023206f14351558585b953835f87d623efc /src/pkg/text/template/exec.go | |
| parent | 3bd8684facfadb57ba649cae6b067e3a3ecb1208 (diff) | |
| download | go-cc842c738ea9a64570e306cbab37c3e3cf9a35dd.tar.xz | |
text/template: allow grouping of pipelines using parentheses
Based on work by Russ Cox. From his CL:
This is generally useful but especially helpful when trying
to use the built-in boolean operators. It lets you write:
{{if not (f 1)}} foo {{end}}
{{if and (f 1) (g 2)}} bar {{end}}
{{if or (f 1) (g 2)}} quux {{end}}
instead of
{{if f 1 | not}} foo {{end}}
{{if f 1}}{{if g 2}} bar {{end}}{{end}}
{{$do := 0}}{{if f 1}}{{$do := 1}}{{else if g 2}}{{$do := 1}}{{end}}{{if $do}} quux {{end}}
The result can be a bit LISPy but the benefit in expressiveness and readability
for such a small change justifies it.
I believe no changes are required to html/template.
Fixes #3276.
R=golang-dev, adg, rogpeppe, minux.ma
CC=golang-dev
https://golang.org/cl/6482056
Diffstat (limited to 'src/pkg/text/template/exec.go')
| -rw-r--r-- | src/pkg/text/template/exec.go | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/src/pkg/text/template/exec.go b/src/pkg/text/template/exec.go index a041351448..1739a86179 100644 --- a/src/pkg/text/template/exec.go +++ b/src/pkg/text/template/exec.go @@ -564,6 +564,8 @@ func (s *state) evalArg(dot reflect.Value, typ reflect.Type, n parse.Node) refle return s.validateType(s.evalFieldNode(dot, arg, []parse.Node{n}, zero), typ) case *parse.VariableNode: return s.validateType(s.evalVariableNode(dot, arg, nil, zero), typ) + case *parse.PipeNode: + return s.validateType(s.evalPipeline(dot, arg), typ) } switch typ.Kind() { case reflect.Bool: @@ -666,6 +668,8 @@ func (s *state) evalEmptyInterface(dot reflect.Value, n parse.Node) reflect.Valu return reflect.ValueOf(n.Text) case *parse.VariableNode: return s.evalVariableNode(dot, n, nil, zero) + case *parse.PipeNode: + return s.evalPipeline(dot, n) } s.errorf("can't handle assignment of %s to empty interface argument", n) panic("not reached") |
