add template func Substr function
Showing
1 changed file
with
15 additions
and
0 deletions
| ... | @@ -29,6 +29,7 @@ func init() { | ... | @@ -29,6 +29,7 @@ func init() { |
| 29 | beegoTplFuncMap["dateformat"] = DateFormat | 29 | beegoTplFuncMap["dateformat"] = DateFormat |
| 30 | beegoTplFuncMap["date"] = Date | 30 | beegoTplFuncMap["date"] = Date |
| 31 | beegoTplFuncMap["compare"] = Compare | 31 | beegoTplFuncMap["compare"] = Compare |
| 32 | beegoTplFuncMap["substr"] = Substr | ||
| 32 | } | 33 | } |
| 33 | 34 | ||
| 34 | // MarkDown parses a string in MarkDown format and returns HTML. Used by the template parser as "markdown" | 35 | // 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) { | ... | @@ -39,6 +40,20 @@ func MarkDown(raw string) (output template.HTML) { |
| 39 | return | 40 | return |
| 40 | } | 41 | } |
| 41 | 42 | ||
| 43 | func Substr(s string, start, length int) string { | ||
| 44 | bt := []rune(s) | ||
| 45 | if start < 0 { | ||
| 46 | start = 0 | ||
| 47 | } | ||
| 48 | var end int | ||
| 49 | if (start + length) > (len(bt) - 1) { | ||
| 50 | end = len(bt) - 1 | ||
| 51 | } else { | ||
| 52 | end = start + length | ||
| 53 | } | ||
| 54 | return string(bt[start:end]) | ||
| 55 | } | ||
| 56 | |||
| 42 | // DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" | 57 | // DateFormat takes a time and a layout string and returns a string with the formatted date. Used by the template parser as "dateformat" |
| 43 | func DateFormat(t time.Time, layout string) (datestring string) { | 58 | func DateFormat(t time.Time, layout string) (datestring string) { |
| 44 | datestring = t.Format(layout) | 59 | datestring = t.Format(layout) | ... | ... |
-
Please register or sign in to post a comment