mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
parent
4a6bc4aac4
commit
675af529c9
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
|
||||
|
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