Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tpl
diff options
context:
space:
mode:
authorWade Fitzpatrick <wade.fitzpatrick@gmail.com>2016-08-04 02:55:24 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-08-04 02:55:24 +0300
commit70544f9e62e5577d1daac1af8849739e4bef98ad (patch)
tree6a7ed4fb56050ce31c183844211335e023fe0859 /tpl
parentd7f364c27e3adc4e012ee07e71f1c5ad5cfeb56f (diff)
tpl: Add time template func
Fixes #2328
Diffstat (limited to 'tpl')
-rw-r--r--tpl/template_funcs.go11
-rw-r--r--tpl/template_funcs_test.go2
2 files changed, 13 insertions, 0 deletions
diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go
index b04b36810..28ac1f885 100644
--- a/tpl/template_funcs.go
+++ b/tpl/template_funcs.go
@@ -1380,6 +1380,16 @@ func replaceRE(pattern, repl, src interface{}) (_ string, err error) {
return re.ReplaceAllString(srcStr, replStr), nil
}
+// asTime converts the textual representation of the datetime string into
+// a time.Time interface.
+func asTime(v interface{}) (interface{}, error) {
+ t, err := cast.ToTimeE(v)
+ if err != nil {
+ return nil, err
+ }
+ return t, 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
@@ -1869,6 +1879,7 @@ func init() {
"sub": func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '-') },
"substr": substr,
"title": func(a string) string { return strings.Title(a) },
+ "time": asTime,
"trim": trim,
"upper": func(a string) string { return strings.ToUpper(a) },
"urlize": helpers.URLize,
diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go
index 2d8f753bd..6246bba0b 100644
--- a/tpl/template_funcs_test.go
+++ b/tpl/template_funcs_test.go
@@ -136,6 +136,7 @@ sub: {{sub 3 2}}
substr: {{substr "BatMan" 0 -3}}
substr: {{substr "BatMan" 3 3}}
title: {{title "Bat man"}}
+time: {{ time "2015-01-21" }}
trim: {{ trim "++Batman--" "+-" }}
upper: {{upper "BatMan"}}
urlize: {{ "Bat Man" | urlize }}
@@ -199,6 +200,7 @@ sub: 1
substr: Bat
substr: Man
title: Bat Man
+time: 2015-01-21T00:00:00Z
trim: Batman
upper: BATMAN
urlize: bat-man