-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplates.go
71 lines (64 loc) · 1.82 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"embed"
"github.com/gin-gonic/gin"
"golang.org/x/text/language"
"golang.org/x/text/message"
"html/template"
"log"
"strconv"
"time"
)
//go:embed files/templates
var templatesFs embed.FS
func loadTemplates(engine *gin.Engine, pattern string) {
printer := message.NewPrinter(language.English)
location := time.Now().Location()
funcMap := template.FuncMap{
"number": func(number any, args ...string) template.HTML {
numberFormatted := printer.Sprintf("%d", number)
if len(args) == 0 {
return template.HTML(numberFormatted)
}
unit := args[0]
if numberFormatted != "1" {
unit += "s"
}
return htmlValue(numberFormatted, unit)
},
"ordinal": func(ordinal int) template.HTML {
return template.HTML(strconv.Itoa(ordinal) + "<sup>" + ordinalSuffix(ordinal) + "</sup>")
},
"currency": func(amount any, currency Currency) template.HTML {
return htmlValue(printer.Sprintf("%.2f", amount), currencyCode(currency))
},
"currencyCode": currencyCode,
"date": func(date time.Time) string {
return date.In(location).Format("02/01/2006")
},
"time": func(date time.Time) string {
return date.In(location).Format("15:04")
},
"datetime": func(date time.Time) string {
return date.In(location).Format("02/01/2006 15:04")
},
"inc": func(number int) int {
return number + 1
},
}
defaultTemplate := template.New("templates").Funcs(funcMap)
completeTemplate, err := defaultTemplate.ParseFS(templatesFs, pattern)
if err != nil {
log.Fatal(err)
}
engine.SetHTMLTemplate(completeTemplate)
}
func htmlValue(value string, unit string) template.HTML {
return template.HTML("<strong>" + value + "</strong> " + unit)
}
func ordinalSuffix(ordinal int) string {
if i := ((ordinal+90)%100-10)%10 - 1; i >= 0 && i < 3 {
return []string{"st", "nd", "rd"}[i]
}
return "th"
}