forked from mirror/gin
update template example (#1038)
* update template example * add template example folder
This commit is contained in:
parent
7b508186dd
commit
d39ed41ab3
43
README.md
43
README.md
|
@ -791,31 +791,42 @@ You may use custom delims
|
||||||
|
|
||||||
#### Custom Template Funcs
|
#### Custom Template Funcs
|
||||||
|
|
||||||
|
See the detail [example code](examples/template).
|
||||||
|
|
||||||
main.go
|
main.go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
...
|
import (
|
||||||
|
"fmt"
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
func formatAsDate(t time.Time) string {
|
"github.com/gin-gonic/gin"
|
||||||
year, month, day := t.Date()
|
)
|
||||||
return fmt.Sprintf("%d/%02d/%02d", year, month, day)
|
|
||||||
}
|
|
||||||
|
|
||||||
...
|
func formatAsDate(t time.Time) string {
|
||||||
|
year, month, day := t.Date()
|
||||||
|
return fmt.Sprintf("%d%02d/%02d", year, month, day)
|
||||||
|
}
|
||||||
|
|
||||||
router.SetFuncMap(template.FuncMap{
|
func main() {
|
||||||
"formatAsDate": formatAsDate,
|
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.GET("/raw", func(c *Context) {
|
router.Run(":8080")
|
||||||
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
|
}
|
||||||
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
...
|
|
||||||
```
|
```
|
||||||
|
|
||||||
raw.tmpl
|
raw.tmpl
|
||||||
|
|
|
@ -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")
|
||||||
|
}
|
Loading…
Reference in New Issue