1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 13:20:55 +00:00

add template func Substr function

This commit is contained in:
astaxie 2013-03-14 21:47:39 +08:00
parent 31f505bfc1
commit b6df895df8

View File

@ -29,6 +29,7 @@ func init() {
beegoTplFuncMap["dateformat"] = DateFormat beegoTplFuncMap["dateformat"] = DateFormat
beegoTplFuncMap["date"] = Date beegoTplFuncMap["date"] = Date
beegoTplFuncMap["compare"] = Compare beegoTplFuncMap["compare"] = Compare
beegoTplFuncMap["substr"] = Substr
} }
// MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown" // 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 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" // 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) { func DateFormat(t time.Time, layout string) (datestring string) {
datestring = t.Format(layout) datestring = t.Format(layout)