-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
51 lines (41 loc) · 1.21 KB
/
template.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
package main
import (
"bytes"
"html/template"
"os"
"time"
"./netutil"
)
var templatesPath = os.Getenv("TEMPLATE_S3_BUCKET")
// GenerateMessage creates main message
func GenerateMessage(blocks []string) string {
var htmlBlocks []template.HTML
htmlBlocks = append(htmlBlocks, template.HTML(GenerateHeader()))
for _, text := range blocks {
htmlBlocks = append(htmlBlocks, template.HTML(text))
}
return executeTemplate("main.html", htmlBlocks)
}
// GenerateHeader creates header HTML for feed
func GenerateHeader() string {
date := time.Now().Format("January 2, 2006")
return executeTemplate("header.html", date)
}
// GenerateHTMLFeedBlock create HTML template for one feed
func GenerateHTMLFeedBlock(feed Feed) string {
return executeTemplate("feed.html", feed)
}
func executeTemplate(templateName string, data interface{}) string {
templateHTML := netutil.ReadFile(templatesPath + templateName)
tmpl, err := template.New(templateName).Parse(string(templateHTML))
if err != nil {
panic(err)
}
var doc bytes.Buffer
err = tmpl.Execute(&doc, data)
return doc.String()
}
// GetCurrentDate returns current date as formatted string
func GetCurrentDate() string {
return time.Now().Format("January 2, 2020")
}