Skip to content
Myr

update template example (#1038)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* update template example * add template example folder

authored by 田欧 · committed by Bo-Yi Wu · parent 7b50818 · Browse files · 2 files+65 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+65 added−22 removed

  • other:+65 added−22 removed

2 files+65 added−22 removed

added:examples/template/main.go+32 added
1package main23import (4 "fmt"5 "html/template"6 "net/http"7 "time"89 "github.com/gin-gonic/gin"10)1112func formatAsDate(t time.Time) string {13 year, month, day := t.Date()14 return fmt.Sprintf("%d%02d/%02d", year, month, day)15}1617func main() {18 router := gin.Default()19 router.Delims("{[{", "}]}")20 router.SetFuncMap(template.FuncMap{21 "formatAsDate": formatAsDate,22 })23 router.LoadHTMLFiles("../../fixtures/basic/raw.tmpl")2425 router.GET("/raw", func(c *gin.Context) {26 c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{27 "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),28 })29 })3031 router.Run(":8080")32}
modified:README.md+33 added-22 removed
789 unmodified lines
790``` 791792#### Custom Template Funcs793794See the detail [example code](examples/template).795796main.go797798```go797 ...798 799 func formatAsDate(t time.Time) string {800 year, month, day := t.Date()801 return fmt.Sprintf("%d/%02d/%02d", year, month, day)802 }803 804 ...805 806 router.SetFuncMap(template.FuncMap{807 "formatAsDate": formatAsDate,808 })809 810 ...811 812 router.GET("/raw", func(c *Context) {813 c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{814 "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),815 })816 })817 818 ...799import (800 "fmt"801 "html/template"802 "net/http"803 "time"804805 "github.com/gin-gonic/gin"806)807808func formatAsDate(t time.Time) string {809 year, month, day := t.Date()810 return fmt.Sprintf("%d%02d/%02d", year, month, day)811}812813func main() {814 router := gin.Default()815 router.Delims("{[{", "}]}")816 router.SetFuncMap(template.FuncMap{817 "formatAsDate": formatAsDate,818 })819 router.LoadHTMLFiles("./fixtures/basic/raw.tmpl")820821 router.GET("/raw", func(c *gin.Context) {822 c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{823 "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),824 })825 })826827 router.Run(":8080")828}829830```831832raw.tmpl833
303 unmodified lines