add template func Substr function

This commit is contained in:
astaxie 2013-03-14 21:47:39 +08:00
parent 31f505bfc1
commit b6df895df8
1 changed files with 15 additions and 0 deletions

View File

@ -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)