-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.go
More file actions
40 lines (33 loc) · 863 Bytes
/
templates.go
File metadata and controls
40 lines (33 loc) · 863 Bytes
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
package main
import (
"github.com/labstack/echo"
"html/template"
"io"
"strings"
"time"
)
type Template struct {
Template *template.Template
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.Template.ExecuteTemplate(w, name, data)
}
func loadTemplate() *Template {
templateFunctions := template.FuncMap{
"nl2br": nl2br,
"date": date,
}
t := &Template{
Template: template.Must(template.New("").Funcs(templateFunctions).ParseGlob("src/notes/templates/*.html")),
}
return t
}
// replace "\n" to "<br>"
func nl2br(text string) template.HTML {
return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
}
// format date
func date(dt time.Time) string {
location := time.FixedZone("Asia/Tokyo", 9*60*60)
return dt.In(location).Format("2006/01/02 15:04")
}