mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 04:57:07 +08:00
gin and pongo2 template engine example
This commit is contained in:
parent
35fd7fb480
commit
236cac7fdf
36
examples/pong2-render/main.go
Normal file
36
examples/pong2-render/main.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/flosch/pongo2"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
r.HTMLRender = NewNgPongRender()
|
||||||
|
pages := r.Group("/")
|
||||||
|
{
|
||||||
|
pages.GET("/", func(c *gin.Context) {
|
||||||
|
ctx := pongo2.Context{
|
||||||
|
"hello": "Hello Home Pages",
|
||||||
|
}
|
||||||
|
c.HTML(200, "templates/sites/home/index.html", ctx)
|
||||||
|
})
|
||||||
|
pages.GET("/user", func(c *gin.Context) {
|
||||||
|
ctx := pongo2.Context{
|
||||||
|
"hello": "Hello User Pages",
|
||||||
|
}
|
||||||
|
c.HTML(200, "templates/sites/user/index.html", ctx)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
admins := r.Group("/admin")
|
||||||
|
{
|
||||||
|
admins.GET("/", func(c *gin.Context) {
|
||||||
|
ctx := pongo2.Context{
|
||||||
|
"hello": "Hello Admin Dashboard Pages",
|
||||||
|
}
|
||||||
|
c.HTML(200, "templates/admins/dashboard/index.html", ctx)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
r.Run(":8080")
|
||||||
|
}
|
48
examples/pong2-render/pongo2.go
Normal file
48
examples/pong2-render/pongo2.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/flosch/pongo2"
|
||||||
|
"github.com/gin-gonic/gin/render"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NgHTML struct {
|
||||||
|
Template map[string]*pongo2.Template
|
||||||
|
Name string
|
||||||
|
Data interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n NgHTML) Write(w http.ResponseWriter) error {
|
||||||
|
file := n.Name
|
||||||
|
ctx := n.Data.(pongo2.Context)
|
||||||
|
|
||||||
|
var t *pongo2.Template
|
||||||
|
|
||||||
|
if tmpl, ok := n.Template[file]; ok {
|
||||||
|
t = tmpl
|
||||||
|
} else {
|
||||||
|
tmpl, err := pongo2.FromFile(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n.Template[file] = tmpl
|
||||||
|
t = tmpl
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
return t.ExecuteWriter(ctx, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
type NgPongRender struct {
|
||||||
|
Template map[string]*pongo2.Template
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NgPongRender) Instance(name string, data interface{}) render.Render {
|
||||||
|
return NgHTML{
|
||||||
|
Template: n.Template,
|
||||||
|
Name: name,
|
||||||
|
Data: data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func NewNgPongRender() *NgPongRender {
|
||||||
|
return &NgPongRender{Template: map[string]*pongo2.Template{}}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
<footer>
|
||||||
|
<h3>Footer Admin</h3>
|
||||||
|
</footer>
|
@ -0,0 +1,3 @@
|
|||||||
|
<header>
|
||||||
|
<h1>Header Admin</h1>
|
||||||
|
</header>
|
14
examples/pong2-render/templates/admins/common/layout.html
Normal file
14
examples/pong2-render/templates/admins/common/layout.html
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{% include "header.html" %}
|
||||||
|
|
||||||
|
{%block content%}
|
||||||
|
{%endblock%}
|
||||||
|
|
||||||
|
{% include "footer.html"%}
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,4 @@
|
|||||||
|
{% extends "layout.html" %}
|
||||||
|
{%block view%}
|
||||||
|
<h1>{{hello}}</h1>
|
||||||
|
{%endblock%}
|
@ -0,0 +1,6 @@
|
|||||||
|
{% extends "../common/layout.html" %}
|
||||||
|
{%block content%}
|
||||||
|
{%block view%}
|
||||||
|
|
||||||
|
{%endblock%}
|
||||||
|
{%endblock%}
|
3
examples/pong2-render/templates/sites/common/footer.html
Normal file
3
examples/pong2-render/templates/sites/common/footer.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<footer>
|
||||||
|
<h3>Footer Site</h3>
|
||||||
|
</footer>
|
3
examples/pong2-render/templates/sites/common/header.html
Normal file
3
examples/pong2-render/templates/sites/common/header.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<header>
|
||||||
|
<h1>Header Site</h1>
|
||||||
|
</header>
|
14
examples/pong2-render/templates/sites/common/layout.html
Normal file
14
examples/pong2-render/templates/sites/common/layout.html
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{% include "header.html" %}
|
||||||
|
|
||||||
|
{%block content%}
|
||||||
|
{%endblock%}
|
||||||
|
|
||||||
|
{% include "footer.html"%}
|
||||||
|
</body>
|
||||||
|
</html>
|
4
examples/pong2-render/templates/sites/home/index.html
Normal file
4
examples/pong2-render/templates/sites/home/index.html
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{% extends "layout.html" %}
|
||||||
|
{%block view%}
|
||||||
|
<h1>{{hello}}</h1>
|
||||||
|
{%endblock%}
|
6
examples/pong2-render/templates/sites/home/layout.html
Normal file
6
examples/pong2-render/templates/sites/home/layout.html
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{% extends "../common/layout.html" %}
|
||||||
|
{%block content%}
|
||||||
|
{%block view%}
|
||||||
|
|
||||||
|
{%endblock%}
|
||||||
|
{%endblock%}
|
4
examples/pong2-render/templates/sites/user/index.html
Normal file
4
examples/pong2-render/templates/sites/user/index.html
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{% extends "layout.html" %}
|
||||||
|
{%block view%}
|
||||||
|
<h1>{{hello}}</h1>
|
||||||
|
{%endblock%}
|
6
examples/pong2-render/templates/sites/user/layout.html
Normal file
6
examples/pong2-render/templates/sites/user/layout.html
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{% extends "../common/layout.html" %}
|
||||||
|
{%block content%}
|
||||||
|
{%block view%}
|
||||||
|
|
||||||
|
{%endblock%}
|
||||||
|
{%endblock%}
|
Loading…
x
Reference in New Issue
Block a user