From 18659e16ba32a9b925ce8c6e270dce13d0881a36 Mon Sep 17 00:00:00 2001 From: "Black.Lee" Date: Mon, 5 Jan 2015 16:38:57 +0800 Subject: [PATCH] add compare_not/not_nil methods for template --- template.go | 3 +++ templatefunc.go | 8 ++++++++ templatefunc_test.go | 11 ++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/template.go b/template.go index 2ca84f22..64b1939e 100644 --- a/template.go +++ b/template.go @@ -42,6 +42,9 @@ func init() { beegoTplFuncMap["dateformat"] = DateFormat beegoTplFuncMap["date"] = Date beegoTplFuncMap["compare"] = Compare + beegoTplFuncMap["compare_not"] = CompareNot + beegoTplFuncMap["not_nil"] = NotNil + beegoTplFuncMap["not_null"] = NotNil beegoTplFuncMap["substr"] = Substr beegoTplFuncMap["html2str"] = Html2str beegoTplFuncMap["str2html"] = Str2html diff --git a/templatefunc.go b/templatefunc.go index 16067613..7d3ac6c9 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -139,6 +139,14 @@ func Compare(a, b interface{}) (equal bool) { return } +func CompareNot(a, b interface{}) (equal bool) { + return ! Compare(a, b) +} + +func NotNil(a interface{}) (is_nil bool) { + return CompareNot(a, nil) +} + func Config(returnType, key string, defaultVal interface{}) (value interface{}, err error) { switch returnType { case "String": diff --git a/templatefunc_test.go b/templatefunc_test.go index 3692a821..60af5bf5 100644 --- a/templatefunc_test.go +++ b/templatefunc_test.go @@ -72,7 +72,7 @@ func TestDate(t *testing.T) { } } -func TestCompare(t *testing.T) { +func TestCompareRelated(t *testing.T) { if !Compare("abc", "abc") { t.Error("should be equal") } @@ -82,6 +82,15 @@ func TestCompare(t *testing.T) { if !Compare("1", 1) { t.Error("should be equal") } + if CompareNot("abc", "abc") { + t.Error("should be equal") + } + if !CompareNot("abc", "aBc") { + t.Error("should be not equal") + } + if !NotNil("a string") { + t.Error("should not be nil") + } } func TestHtmlquote(t *testing.T) {