Files
7days-golang/day6-template/main.go
T
2019-08-18 01:58:26 +08:00

85 lines
1.4 KiB
Go

package main
/*
(1) render array
$ curl http://localhost:9999/date
<html>
<body>
<p>hello, gee</p>
<p>Date: 2019-08-17</p>
</body>
</html>
*/
/*
(2) custom render function
$ curl http://localhost:9999/students
<html>
<body>
<p>hello, gee</p>
<p>0: Geektutu is 20 years old</p>
<p>1: Jack is 22 years old</p>
</body>
</html>
*/
/*
(3) serve static files
$ curl http://localhost:9999/assets/geektutu.css
p {
color: orange;
font-weight: 700;
font-size: 20px;
}
*/
import (
"fmt"
"html/template"
"net/http"
"time"
"./gee"
)
type student struct {
Name string
Age int8
}
func formatAsDate(t time.Time) string {
year, month, day := t.Date()
return fmt.Sprintf("%d-%02d-%02d", year, month, day)
}
func main() {
r := gee.New()
r.Use(gee.Logger())
r.SetFuncMap(template.FuncMap{
"formatAsDate": formatAsDate,
})
r.LoadHTMLGlob("templates/*")
r.Static("/assets", "./static")
stu1 := &student{Name: "Geektutu", Age: 20}
stu2 := &student{Name: "Jack", Age: 22}
r.GET("/", func(c *gee.Context) {
c.HTML(http.StatusOK, "css.tmpl", nil)
})
r.GET("/students", func(c *gee.Context) {
c.HTML(http.StatusOK, "arr.tmpl", gee.H{
"title": "gee",
"stuArr": [2]*student{stu1, stu2},
})
})
r.GET("/date", func(c *gee.Context) {
c.HTML(http.StatusOK, "custom_func.tmpl", gee.H{
"title": "gee",
"now": time.Date(2019, 8, 17, 0, 0, 0, 0, time.UTC),
})
})
r.Run(":9999")
}