Add Pluggable View Renderer Example
Gin meets pongo2! (https://github.com/flosch/pongo2)
This commit is contained in:
parent
3cf2d1338f
commit
2c3cdbb69f
58
examples/pluggable_renderer/example_pongo2.go
Normal file
58
examples/pluggable_renderer/example_pongo2.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/flosch/pongo2"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pongoRender struct {
|
||||||
|
cache map[string]*pongo2.Template
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPongoRender() *pongoRender {
|
||||||
|
return &pongoRender{map[string]*pongo2.Template{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeHeader(w http.ResponseWriter, code int, contentType string) {
|
||||||
|
if code >= 0 {
|
||||||
|
w.Header().Set("Content-Type", contentType)
|
||||||
|
w.WriteHeader(code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
writeHeader(w, code, "text/html")
|
||||||
|
return t.ExecuteRW(w, ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
r.HTMLRender = newPongoRender()
|
||||||
|
|
||||||
|
r.GET("/index", func(c *gin.Context) {
|
||||||
|
name := c.Request.FormValue("name")
|
||||||
|
ctx := pongo2.Context{
|
||||||
|
"title": "Gin meets pongo2 !",
|
||||||
|
"name": name,
|
||||||
|
}
|
||||||
|
c.HTML(200, "index.html", ctx)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Listen and server on 0.0.0.0:8080
|
||||||
|
r.Run(":8080")
|
||||||
|
}
|
12
examples/pluggable_renderer/index.html
Normal file
12
examples/pluggable_renderer/index.html
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ja">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>{{ title }}</title>
|
||||||
|
<meta name="keywords" content="">
|
||||||
|
<meta name="description" content="">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
Hello {{ name }} !
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user