Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e954823

Browse filesBrowse files
authored
Merge pull request revel#1207 from wariosolis/develop
Timeago Template Func
2 parents 42c9637 + bd534eb commit e954823
Copy full SHA for e954823

File tree

1 file changed

+90
-10
lines changed
Filter options

1 file changed

+90
-10
lines changed

‎template_functions.go

Copy file name to clipboardExpand all lines: template_functions.go
+90-10Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package revel
22

33
import (
4+
"bytes"
5+
"errors"
46
"fmt"
7+
//"github.com/revel/config"
8+
"github.com/xeonx/timeago"
59
"html"
610
"html/template"
711
"reflect"
812
"strings"
913
"time"
10-
"bytes"
11-
"errors"
1214
)
1315

1416
var (
@@ -145,6 +147,9 @@ var (
145147
},
146148
"slug": Slug,
147149
"even": func(a int) bool { return (a % 2) == 0 },
150+
151+
// Using https://github.com/xeonx/timeago
152+
"timeago": TimeAgo,
148153
"i18ntemplate": func(args ...interface{}) (template.HTML, error) {
149154
templateName, lang := "", ""
150155
var viewArgs interface{}
@@ -159,16 +164,16 @@ var (
159164
templateName = args[0].(string)
160165
viewArgs = args[1]
161166
// Try to extract language from the view args
162-
if viewargsmap,ok := viewArgs.(map[string]interface{});ok {
163-
lang,_ = viewargsmap[CurrentLocaleViewArg].(string)
167+
if viewargsmap, ok := viewArgs.(map[string]interface{}); ok {
168+
lang, _ = viewargsmap[CurrentLocaleViewArg].(string)
164169
}
165170
default:
166171
// Assume third argument is the region
167172
templateName = args[0].(string)
168173
viewArgs = args[1]
169174
lang, _ = args[2].(string)
170-
if len(args)>3 {
171-
ERROR.Printf("Received more parameters then needed for %s",templateName)
175+
if len(args) > 3 {
176+
ERROR.Printf("Received more parameters then needed for %s", templateName)
172177
}
173178
}
174179

@@ -178,12 +183,13 @@ var (
178183
if err == nil {
179184
err = tmpl.Render(&buf, viewArgs)
180185
} else {
181-
ERROR.Printf("Failed to render i18ntemplate %s %v",templateName,err)
186+
ERROR.Printf("Failed to render i18ntemplate %s %v", templateName, err)
182187
}
183188
return template.HTML(buf.String()), err
184189
},
185190
}
186191
)
192+
187193
/////////////////////
188194
// Template functions
189195
/////////////////////
@@ -208,13 +214,13 @@ func ReverseURL(args ...interface{}) (template.URL, error) {
208214

209215
// Look up the types.
210216

211-
if pathData.TypeOfController==nil {
217+
if pathData.TypeOfController == nil {
212218
return "", fmt.Errorf("Failed reversing %s: controller not found %#v", action, pathData)
213219
}
214220

215221
// Note method name is case insensitive search
216222
methodType := pathData.TypeOfController.Method(pathData.MethodName)
217-
if methodType == nil {
223+
if methodType == nil {
218224
return "", errors.New("revel/controller: In " + action + " failed to find function " + pathData.MethodName)
219225
}
220226

@@ -228,7 +234,7 @@ func ReverseURL(args ...interface{}) (template.URL, error) {
228234
fixedParams := len(pathData.FixedParamsByName)
229235

230236
for i, argValue := range args[1:] {
231-
Unbind(argsByName, methodType.Args[i + fixedParams].Name, argValue)
237+
Unbind(argsByName, methodType.Args[i+fixedParams].Name, argValue)
232238
}
233239

234240
return template.URL(MainRouter.Reverse(args[0].(string), argsByName).URL), nil
@@ -242,3 +248,77 @@ func Slug(text string) string {
242248
text = strings.Trim(text, separator)
243249
return text
244250
}
251+
252+
var timeAgoLangs = map[string]timeago.Config{}
253+
254+
func TimeAgo(args ...interface{}) string {
255+
256+
datetime := time.Now()
257+
lang := ""
258+
var viewArgs interface{}
259+
switch len(args) {
260+
case 0:
261+
ERROR.Printf("No arguements passed to timeago")
262+
case 1:
263+
// only the time is passed in
264+
datetime = args[0].(time.Time)
265+
case 2:
266+
// time and region is passed in
267+
datetime = args[0].(time.Time)
268+
switch v := reflect.ValueOf(args[1]); v.Kind() {
269+
case reflect.String:
270+
// second params type string equals region
271+
lang, _ = args[1].(string)
272+
case reflect.Map:
273+
// second params type map equals viewArgs
274+
viewArgs = args[1]
275+
if viewargsmap, ok := viewArgs.(map[string]interface{}); ok {
276+
lang, _ = viewargsmap[CurrentLocaleViewArg].(string)
277+
}
278+
default:
279+
ERROR.Println("pluralize: unexpected type: ", v)
280+
}
281+
default:
282+
// Assume third argument is the region
283+
datetime = args[0].(time.Time)
284+
if reflect.ValueOf(args[1]).Kind() != reflect.Map {
285+
ERROR.Println("pluralize: unexpected type: ", args[1])
286+
}
287+
if reflect.ValueOf(args[2]).Kind() != reflect.String {
288+
ERROR.Println("unexpected type: ", args[2])
289+
}
290+
viewArgs = args[1]
291+
lang, _ = args[2].(string)
292+
if len(args) > 3 {
293+
ERROR.Printf("Received more parameters then needed for timeago")
294+
}
295+
}
296+
if lang == "" {
297+
lang, _ = Config.String(defaultLanguageOption)
298+
if lang == "en" {
299+
timeAgoLangs[lang] = timeago.English
300+
}
301+
}
302+
_, ok := timeAgoLangs[lang]
303+
if !ok {
304+
timeAgoLangs[lang] = timeago.Config{
305+
PastPrefix: "",
306+
PastSuffix: " " + MessageFunc(lang, "ago"),
307+
FuturePrefix: MessageFunc(lang, "in") + " ",
308+
FutureSuffix: "",
309+
Periods: []timeago.FormatPeriod{
310+
timeago.FormatPeriod{time.Second, MessageFunc(lang, "about a second"), MessageFunc(lang, "%d seconds")},
311+
timeago.FormatPeriod{time.Minute, MessageFunc(lang, "about a minute"), MessageFunc(lang, "%d minutes")},
312+
timeago.FormatPeriod{time.Hour, MessageFunc(lang, "about an hour"), MessageFunc(lang, "%d hours")},
313+
timeago.FormatPeriod{timeago.Day, MessageFunc(lang, "one day"), MessageFunc(lang, "%d days")},
314+
timeago.FormatPeriod{timeago.Month, MessageFunc(lang, "one month"), MessageFunc(lang, "%d months")},
315+
timeago.FormatPeriod{timeago.Year, MessageFunc(lang, "one year"), MessageFunc(lang, "%d years")},
316+
},
317+
Zero: MessageFunc(lang, "about a second"),
318+
Max: 73 * time.Hour,
319+
DefaultLayout: "2006-01-02",
320+
}
321+
322+
}
323+
return timeAgoLangs[lang].Format(datetime)
324+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.