/
template.go
47 lines (39 loc) · 1.01 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
package main
import (
"fmt"
"text/template"
"flag"
"os"
"encoding/json"
)
var (
pattern string
jsonFile string
mainTemplate string
)
func init() {
flag.StringVar(&pattern, "p", "templates/*.html", "template pattern like template/*.html")
flag.StringVar(&jsonFile, "f", "", "JSON file which is embeded in templates.")
flag.StringVar(&mainTemplate, "t", "index", "main template like index")
}
const SIZE = 1000000
func main() {
flag.Parse()
template, err := template.ParseGlob(pattern)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return
}
file,_ := os.Open(jsonFile)
buff := make([]byte, SIZE)
count,_ := file.Read(buff)
jsonData := buff[:count]
data := make(map[string]interface{})
if string(jsonData) != "" {
if err := json.Unmarshal(jsonData, &data); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return
}
}
template.ExecuteTemplate(os.Stdout, mainTemplate, data)
}