From b6df895df80955744d751c3ad937eea4273bce52 Mon Sep 17 00:00:00 2001 From: astaxie Date: Thu, 14 Mar 2013 21:47:39 +0800 Subject: [PATCH] add template func Substr function --- template.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/template.go b/template.go index 5d2c4fa3..c513567d 100644 --- a/template.go +++ b/template.go @@ -29,6 +29,7 @@ func init() { beegoTplFuncMap["dateformat"] = DateFormat beegoTplFuncMap["date"] = Date beegoTplFuncMap["compare"] = Compare + beegoTplFuncMap["substr"] = Substr } // MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown" @@ -39,6 +40,20 @@ func MarkDown(raw string) (output template.HTML) { return } +func Substr(s string, start, length int) string { + bt := []rune(s) + if start < 0 { + start = 0 + } + var end int + if (start + length) > (len(bt) - 1) { + end = len(bt) - 1 + } else { + end = start + length + } + return string(bt[start:end]) +} + // DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" func DateFormat(t time.Time, layout string) (datestring string) { datestring = t.Format(layout)