mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 02:32:17 +08:00
update template example
This commit is contained in:
parent
7b508186dd
commit
ddc67e2abc
53
README.md
53
README.md
@ -794,28 +794,37 @@ You may use custom delims
|
|||||||
main.go
|
main.go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
...
|
import (
|
||||||
|
"fmt"
|
||||||
func formatAsDate(t time.Time) string {
|
"html/template"
|
||||||
year, month, day := t.Date()
|
"net/http"
|
||||||
return fmt.Sprintf("%d/%02d/%02d", year, month, day)
|
"time"
|
||||||
}
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
...
|
)
|
||||||
|
|
||||||
router.SetFuncMap(template.FuncMap{
|
func formatAsDate(t time.Time) string {
|
||||||
"formatAsDate": formatAsDate,
|
year, month, day := t.Date()
|
||||||
})
|
return fmt.Sprintf("%d%02d/%02d", year, month, day)
|
||||||
|
}
|
||||||
...
|
|
||||||
|
func main() {
|
||||||
router.GET("/raw", func(c *Context) {
|
router := gin.Default()
|
||||||
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
|
router.Delims("{[{", "}]}")
|
||||||
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
raw.tmpl
|
raw.tmpl
|
||||||
|
Loading…
x
Reference in New Issue
Block a user