add template example folder

This commit is contained in:
thinkerou 2017-07-20 23:32:30 +08:00
parent ddc67e2abc
commit a1f19ec2d0
2 changed files with 34 additions and 0 deletions

View File

@ -791,6 +791,8 @@ You may use custom delims
#### Custom Template Funcs
See the detail [example code](examples/template).
main.go
```go

32
examples/template/main.go Normal file
View File

@ -0,0 +1,32 @@
package main
import (
"fmt"
"html/template"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func formatAsDate(t time.Time) string {
year, month, day := t.Date()
return fmt.Sprintf("%d%02d/%02d", year, month, day)
}
func main() {
router := gin.Default()
router.Delims("{[{", "}]}")
router.SetFuncMap(template.FuncMap{
"formatAsDate": formatAsDate,
})
router.LoadHTMLFiles("../../fixtures/basic/raw.tmpl")
router.GET("/raw", func(c *gin.Context) {
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
})
})
router.Run(":8080")
}