gin and pongo2 template engine example

This commit is contained in:
Nguyen Nguyen 2015-05-23 17:48:01 +07:00
parent 35fd7fb480
commit 236cac7fdf
14 changed files with 154 additions and 0 deletions

View 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")
}

View 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{}}
}

View File

@ -0,0 +1,3 @@
<footer>
<h3>Footer Admin</h3>
</footer>

View File

@ -0,0 +1,3 @@
<header>
<h1>Header Admin</h1>
</header>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
{% include "header.html" %}
{%block content%}
{%endblock%}
{% include "footer.html"%}
</body>
</html>

View File

@ -0,0 +1,4 @@
{% extends "layout.html" %}
{%block view%}
<h1>{{hello}}</h1>
{%endblock%}

View File

@ -0,0 +1,6 @@
{% extends "../common/layout.html" %}
{%block content%}
{%block view%}
{%endblock%}
{%endblock%}

View File

@ -0,0 +1,3 @@
<footer>
<h3>Footer Site</h3>
</footer>

View File

@ -0,0 +1,3 @@
<header>
<h1>Header Site</h1>
</header>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
{% include "header.html" %}
{%block content%}
{%endblock%}
{% include "footer.html"%}
</body>
</html>

View File

@ -0,0 +1,4 @@
{% extends "layout.html" %}
{%block view%}
<h1>{{hello}}</h1>
{%endblock%}

View File

@ -0,0 +1,6 @@
{% extends "../common/layout.html" %}
{%block content%}
{%block view%}
{%endblock%}
{%endblock%}

View File

@ -0,0 +1,4 @@
{% extends "layout.html" %}
{%block view%}
<h1>{{hello}}</h1>
{%endblock%}

View File

@ -0,0 +1,6 @@
{% extends "../common/layout.html" %}
{%block content%}
{%block view%}
{%endblock%}
{%endblock%}