Add dateFormat template function

This is the implementation of the proposal at #235 and
http://discuss.gohugo.io/t/parsing-dates-in-templates/603/3
This commit is contained in:
Tatsushi Demachi
2015-01-21 22:52:12 +09:00
committed by bep
parent 878754c21f
commit 37490ee27a
3 changed files with 47 additions and 0 deletions

View File

@@ -905,6 +905,17 @@ func Replace(a, b, c interface{}) (string, error) {
return strings.Replace(aStr, bStr, cStr, -1), nil
}
// DateFormat converts the textual representation of the datetime string into
// the other form or returns it of the time.Time value. These are formatted
// with the layout string
func DateFormat(layout string, v interface{}) (string, error) {
t, err := cast.ToTimeE(v)
if err != nil {
return "", err
}
return t.Format(layout), nil
}
func SafeHtml(text string) template.HTML {
return template.HTML(text)
}
@@ -1281,6 +1292,7 @@ func init() {
"chomp": Chomp,
"replace": Replace,
"trim": Trim,
"dateFormat": DateFormat,
}
}