mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 05:16:35 +08:00
Merge 2aea02ecb8735702d83406777d525dd7fe29146a into 72ffff6e442e76b3109322d71a2b64a1ba0fcf74
This commit is contained in:
commit
affc18fb13
10
examples/template-blocks/layouts/default.html
Normal file
10
examples/template-blocks/layouts/default.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
{{block "head" .}} {{end}}
|
||||
</head>
|
||||
<body>
|
||||
{{block "body" .}} {{end}}
|
||||
</body>
|
||||
</html>
|
19
examples/template-blocks/main.go
Normal file
19
examples/template-blocks/main.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
|
||||
router.AddHTMLTemplate("/", "layouts/default.html", "views/blocks1.html")
|
||||
router.AddHTMLTemplate("/blocks2/", "layouts/default.html", "views/blocks2.html")
|
||||
|
||||
router.GET("/", render)
|
||||
router.GET("/blocks2/", render)
|
||||
router.Run(":8080")
|
||||
|
||||
}
|
||||
|
||||
func render(c *gin.Context) {
|
||||
c.HTML(200, c.Request.URL.Path, gin.H{})
|
||||
}
|
7
examples/template-blocks/views/blocks1.html
Normal file
7
examples/template-blocks/views/blocks1.html
Normal file
@ -0,0 +1,7 @@
|
||||
{{define "head"}}
|
||||
<title>Blocks 1</title>
|
||||
{{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<a href="/blocks2/">Go to blocks 2</a>
|
||||
{{end}}
|
7
examples/template-blocks/views/blocks2.html
Normal file
7
examples/template-blocks/views/blocks2.html
Normal file
@ -0,0 +1,7 @@
|
||||
{{define "head"}}
|
||||
<title>Blocks 2</title>
|
||||
{{end}}
|
||||
|
||||
{{define "body"}}
|
||||
<a href="/">Go to blocks 1</a>
|
||||
{{end}}
|
16
gin.go
16
gin.go
@ -147,6 +147,22 @@ func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
|
||||
engine.HTMLRender = render.HTMLProduction{Template: templ}
|
||||
}
|
||||
|
||||
// AddHTMLTemplate allow to use "block" template feature from go1.6
|
||||
func (engine *Engine) AddHTMLTemplate(name string, paths... string) {
|
||||
var templateStorage render.TemplateStorage
|
||||
t, ok := engine.HTMLRender.(render.TemplateStorage)
|
||||
if ok {
|
||||
templateStorage = t
|
||||
} else {
|
||||
if len(engine.trees) > 0 {
|
||||
debugPrintWARNINGSetHTMLTemplate()
|
||||
}
|
||||
templateStorage = render.TemplateStorage{make(map[string]*template.Template)}
|
||||
}
|
||||
templateStorage.Storage[name] = template.Must(template.ParseFiles(paths...))
|
||||
engine.HTMLRender = templateStorage
|
||||
}
|
||||
|
||||
// Adds handlers for NoRoute. It return a 404 code by default.
|
||||
func (engine *Engine) NoRoute(handlers ...HandlerFunc) {
|
||||
engine.noRoute = handlers
|
||||
|
@ -128,3 +128,41 @@ func TestRenderHTMLTemplate(t *testing.T) {
|
||||
assert.Equal(t, w.Body.String(), "Hello alexandernyquist")
|
||||
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
|
||||
}
|
||||
|
||||
func TestRenderHTMLTemplateWithBlocks(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
var indexTemplate = template.Must(template.New("").Parse(`{{block "first" .}}{{end}}{{block "second" .}}{{end}}`))
|
||||
|
||||
first := `{{define "first"}}{{.first}}{{end}}{{define "second"}}_{{.second}}{{end}}`
|
||||
var firstT = template.Must(template.Must(indexTemplate.Clone()).Parse(first))
|
||||
second := `{{define "first"}}{{.first}}{{end}}{{define "second"}}_{{.second}}{{end}}`
|
||||
var secondT = template.Must(template.Must(indexTemplate.Clone()).Parse(second))
|
||||
|
||||
templateStorage := TemplateStorage{make(map[string]*template.Template)}
|
||||
templateStorage.Storage["first"] = firstT
|
||||
templateStorage.Storage["second"] = secondT
|
||||
|
||||
testMap := map[string]interface{}{
|
||||
"second": "second",
|
||||
"first": "first",
|
||||
}
|
||||
|
||||
instance := templateStorage.Instance("first", testMap)
|
||||
|
||||
err := instance.Render(w)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, w.Body.String(), "first_second")
|
||||
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
|
||||
instance = templateStorage.Instance("second", testMap)
|
||||
|
||||
err = instance.Render(w)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, w.Body.String(), "first_second")
|
||||
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
|
||||
}
|
||||
|
33
render/template_storage.go
Normal file
33
render/template_storage.go
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type TemplateStorage struct {
|
||||
Storage map[string]*template.Template
|
||||
}
|
||||
|
||||
func (t TemplateStorage) Instance(name string, data interface{}) Render {
|
||||
return HTMLWithBlock{
|
||||
Template: t.Storage[name],
|
||||
Name: name,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
type HTMLWithBlock struct {
|
||||
Template *template.Template
|
||||
Name string
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
func (r HTMLWithBlock) Render(w http.ResponseWriter) error {
|
||||
writeContentType(w, htmlContentType)
|
||||
return r.Template.Execute(w, r.Data)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user