2014-07-16 19:37:56 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-08 03:58:35 +03:00
|
|
|
"net/http"
|
|
|
|
|
2014-07-16 19:37:56 +04:00
|
|
|
"github.com/flosch/pongo2"
|
|
|
|
"github.com/gin-gonic/gin"
|
2015-04-08 03:58:35 +03:00
|
|
|
"github.com/gin-gonic/gin/render"
|
2014-07-16 19:37:56 +04:00
|
|
|
)
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func main() {
|
|
|
|
router := gin.Default()
|
|
|
|
router.HTMLRender = newPongoRender()
|
|
|
|
|
|
|
|
router.GET("/index", func(c *gin.Context) {
|
|
|
|
c.HTML(200, "index.html", gin.H{
|
|
|
|
"title": "Gin meets pongo2 !",
|
|
|
|
"name": c.Input.Get("name"),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
router.Run(":8080")
|
|
|
|
}
|
|
|
|
|
2014-07-16 19:37:56 +04:00
|
|
|
type pongoRender struct {
|
|
|
|
cache map[string]*pongo2.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPongoRender() *pongoRender {
|
|
|
|
return &pongoRender{map[string]*pongo2.Template{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pongoRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
|
|
|
|
file := data[0].(string)
|
|
|
|
ctx := data[1].(pongo2.Context)
|
|
|
|
var t *pongo2.Template
|
|
|
|
|
|
|
|
if tmpl, ok := p.cache[file]; ok {
|
|
|
|
t = tmpl
|
|
|
|
} else {
|
|
|
|
tmpl, err := pongo2.FromFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.cache[file] = tmpl
|
|
|
|
t = tmpl
|
|
|
|
}
|
2015-04-08 03:58:35 +03:00
|
|
|
render.WriteHeader(w, code, "text/html")
|
2015-01-04 03:48:01 +03:00
|
|
|
return t.ExecuteWriter(ctx, w)
|
2014-07-16 19:37:56 +04:00
|
|
|
}
|