} else if duration.Hours() < 24 { return fmt.Sprintf("%.0f 小时前", duration.Hours()) } t = t.Add(time.Hour * time.Duration(Config.TimeZoneOffset)) return t.Format("2006-01-02 15:04") }, "formatdatetime": func(t time.Time) string { // 格式化时间成 2006-01-02 15:04:05 return t.Add(time.Hour * time.Duration(Config.TimeZoneOffset)).Format("2006-01-02 15:04:05") }, "nl2br": func(text string) template.HTML { return template.HTML(strings.Replace(text, "\n", "<br>", -1)) }, "truncate": func(text string, length int, indicator string) string { return webhelpers.Truncate(text, length, indicator) }, "include": func(filename string, data map[string]interface{}) template.HTML { // 加载局部模板,从 templates 中去寻找 var buf bytes.Buffer t, err := template.ParseFiles("templates/" + filename) if err != nil { panic(err) } err = t.Execute(&buf, data) if err != nil { panic(err) } return template.HTML(buf.Bytes()) },
func (u *Utils) Truncate(html template.HTML, length int) string { text := webhelpers.RemoveFormatting(string(html)) return webhelpers.Truncate(text, length, "...") }