Пример #1
0
		if now.Year() == t.Year() && now.Month()-t.Month() < 12 {
			return fmt.Sprintf("%d 个月前", now.Month()-t.Month())
		}
		if now.Year()-t.Year() < 100 {
			return fmt.Sprintf("%d 年前", now.Year()-t.Year())
		}
		return t.Format("2006-01-02 15:04")
	},
	"formatdatetime": func(t time.Time) string {
		return t.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 helpers.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())
	},
Пример #2
0
Файл: views.go Проект: nosqldb/G
func (u *Utils) Truncate(html template.HTML, length int) string {
	text := helpers.RemoveFormatting(string(html))
	return helpers.Truncate(text, length, "...")
}