mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 18:40:55 +00:00
add template func Substr function
This commit is contained in:
parent
31f505bfc1
commit
b6df895df8
15
template.go
15
template.go
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user