Skip to content

Commit a326c3e

Browse files
committed
text/template: export isTrue
The definition of 'truth' used by if etc. is not trivial to compute, so publish the implementation to allow custom template functions to have the same definition as the template language itself. Fixes #12033. Change-Id: Icdfd6039722d7d3f984ba0905105eb3253e14831 Reviewed-on: https://go-review.googlesource.com/14593 Reviewed-by: Andrew Gerrand <[email protected]>
1 parent 1216e18 commit a326c3e

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/html/template/template.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io"
1010
"io/ioutil"
1111
"path/filepath"
12+
"reflect"
1213
"sync"
1314
"text/template"
1415
"text/template/parse"
@@ -413,3 +414,10 @@ func parseGlob(t *Template, pattern string) (*Template, error) {
413414
}
414415
return parseFiles(t, filenames...)
415416
}
417+
418+
// IsTrue reports whether the value is 'true', in the sense of not the zero of its type,
419+
// and whether the value has a meaningful truth value. This is the definition of
420+
// truth used by if and other such actions.
421+
func IsTrue(val reflect.Value) (truth, ok bool) {
422+
return template.IsTrue(val)
423+
}

src/text/template/exec.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (s *state) walk(dot reflect.Value, node parse.Node) {
242242
func (s *state) walkIfOrWith(typ parse.NodeType, dot reflect.Value, pipe *parse.PipeNode, list, elseList *parse.ListNode) {
243243
defer s.pop(s.mark())
244244
val := s.evalPipeline(dot, pipe)
245-
truth, ok := isTrue(val)
245+
truth, ok := IsTrue(val)
246246
if !ok {
247247
s.errorf("if/with can't use %v", val)
248248
}
@@ -257,9 +257,10 @@ func (s *state) walkIfOrWith(typ parse.NodeType, dot reflect.Value, pipe *parse.
257257
}
258258
}
259259

260-
// isTrue reports whether the value is 'true', in the sense of not the zero of its type,
261-
// and whether the value has a meaningful truth value.
262-
func isTrue(val reflect.Value) (truth, ok bool) {
260+
// IsTrue reports whether the value is 'true', in the sense of not the zero of its type,
261+
// and whether the value has a meaningful truth value. This is the definition of
262+
// truth used by if and other such actions.
263+
func IsTrue(val reflect.Value) (truth, ok bool) {
263264
if !val.IsValid() {
264265
// Something like var x interface{}, never set. It's a form of nil.
265266
return false, true

src/text/template/funcs.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func call(fn interface{}, args ...interface{}) (interface{}, error) {
265265
// Boolean logic.
266266

267267
func truth(a interface{}) bool {
268-
t, _ := isTrue(reflect.ValueOf(a))
268+
t, _ := IsTrue(reflect.ValueOf(a))
269269
return t
270270
}
271271

@@ -300,9 +300,8 @@ func or(arg0 interface{}, args ...interface{}) interface{} {
300300
}
301301

302302
// not returns the Boolean negation of its argument.
303-
func not(arg interface{}) (truth bool) {
304-
truth, _ = isTrue(reflect.ValueOf(arg))
305-
return !truth
303+
func not(arg interface{}) bool {
304+
return !truth(arg)
306305
}
307306

308307
// Comparison.

0 commit comments

Comments
 (0)