mirror of https://github.com/gin-gonic/gin.git
add custom Delims support (#860)
* Revert "Merge pull request #753 from gin-gonic/bug" This reverts commit556287ff08
, reversing changes made to32cab500ec
. * Revert "Merge pull request #744 from aviddiviner/logger-fix" This reverts commitc3bfd69303
, reversing changes made to9177f01c28
. * add custom Delims support * add some test for Delims * remove the empty line for import native package * remove unuseful comments
This commit is contained in:
parent
8295db44ed
commit
35f5df63e6
|
@ -0,0 +1 @@
|
||||||
|
<h1>Hello {[{.name}]}</h1>
|
18
gin.go
18
gin.go
|
@ -45,6 +45,7 @@ type (
|
||||||
// Create an instance of Engine, by using New() or Default()
|
// Create an instance of Engine, by using New() or Default()
|
||||||
Engine struct {
|
Engine struct {
|
||||||
RouterGroup
|
RouterGroup
|
||||||
|
delims render.Delims
|
||||||
HTMLRender render.HTMLRender
|
HTMLRender render.HTMLRender
|
||||||
allNoRoute HandlersChain
|
allNoRoute HandlersChain
|
||||||
allNoMethod HandlersChain
|
allNoMethod HandlersChain
|
||||||
|
@ -119,6 +120,7 @@ func New() *Engine {
|
||||||
UseRawPath: false,
|
UseRawPath: false,
|
||||||
UnescapePathValues: true,
|
UnescapePathValues: true,
|
||||||
trees: make(methodTrees, 0, 9),
|
trees: make(methodTrees, 0, 9),
|
||||||
|
delims: render.Delims{"{{", "}}"},
|
||||||
}
|
}
|
||||||
engine.RouterGroup.engine = engine
|
engine.RouterGroup.engine = engine
|
||||||
engine.pool.New = func() interface{} {
|
engine.pool.New = func() interface{} {
|
||||||
|
@ -138,21 +140,26 @@ func (engine *Engine) allocateContext() *Context {
|
||||||
return &Context{engine: engine}
|
return &Context{engine: engine}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) Delims(left, right string) *Engine {
|
||||||
|
engine.delims = render.Delims{left, right}
|
||||||
|
return engine
|
||||||
|
}
|
||||||
|
|
||||||
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
func (engine *Engine) LoadHTMLGlob(pattern string) {
|
||||||
if IsDebugging() {
|
if IsDebugging() {
|
||||||
debugPrintLoadTemplate(template.Must(template.ParseGlob(pattern)))
|
debugPrintLoadTemplate(template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).ParseGlob(pattern)))
|
||||||
engine.HTMLRender = render.HTMLDebug{Glob: pattern}
|
engine.HTMLRender = render.HTMLDebug{Glob: pattern, Delims: engine.delims}
|
||||||
} else {
|
} else {
|
||||||
templ := template.Must(template.ParseGlob(pattern))
|
templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).ParseGlob(pattern))
|
||||||
engine.SetHTMLTemplate(templ)
|
engine.SetHTMLTemplate(templ)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) LoadHTMLFiles(files ...string) {
|
func (engine *Engine) LoadHTMLFiles(files ...string) {
|
||||||
if IsDebugging() {
|
if IsDebugging() {
|
||||||
engine.HTMLRender = render.HTMLDebug{Files: files}
|
engine.HTMLRender = render.HTMLDebug{Files: files, Delims: engine.delims}
|
||||||
} else {
|
} else {
|
||||||
templ := template.Must(template.ParseFiles(files...))
|
templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).ParseFiles(files...))
|
||||||
engine.SetHTMLTemplate(templ)
|
engine.SetHTMLTemplate(templ)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,6 +168,7 @@ func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
|
||||||
if len(engine.trees) > 0 {
|
if len(engine.trees) > 0 {
|
||||||
debugPrintWARNINGSetHTMLTemplate()
|
debugPrintWARNINGSetHTMLTemplate()
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.HTMLRender = render.HTMLProduction{Template: templ}
|
engine.HTMLRender = render.HTMLProduction{Template: templ}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
60
gin_test.go
60
gin_test.go
|
@ -5,14 +5,60 @@
|
||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func setupHTMLFiles(t *testing.T) func() {
|
||||||
|
go func() {
|
||||||
|
router := New()
|
||||||
|
router.Delims("{[{", "}]}")
|
||||||
|
router.LoadHTMLFiles("./fixtures/basic/hello.tmpl")
|
||||||
|
router.GET("/test", func(c *Context) {
|
||||||
|
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
|
||||||
|
})
|
||||||
|
router.Run(":8888")
|
||||||
|
}()
|
||||||
|
t.Log("waiting 1 second for server startup")
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
return func() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupHTMLGlob(t *testing.T) func() {
|
||||||
|
go func() {
|
||||||
|
router := New()
|
||||||
|
router.Delims("{[{", "}]}")
|
||||||
|
router.LoadHTMLGlob("./fixtures/basic/*")
|
||||||
|
router.GET("/test", func(c *Context) {
|
||||||
|
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
|
||||||
|
})
|
||||||
|
router.Run(":8888")
|
||||||
|
}()
|
||||||
|
t.Log("waiting 1 second for server startup")
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
return func() {}
|
||||||
|
}
|
||||||
|
|
||||||
//TODO
|
//TODO
|
||||||
// func (engine *Engine) LoadHTMLGlob(pattern string) {
|
func TestLoadHTMLGlob(t *testing.T) {
|
||||||
|
td := setupHTMLGlob(t)
|
||||||
|
res, err := http.Get("http://127.0.0.1:8888/test")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := ioutil.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
|
||||||
|
|
||||||
|
td()
|
||||||
|
}
|
||||||
|
|
||||||
// func (engine *Engine) LoadHTMLFiles(files ...string) {
|
// func (engine *Engine) LoadHTMLFiles(files ...string) {
|
||||||
// func (engine *Engine) RunTLS(addr string, cert string, key string) error {
|
// func (engine *Engine) RunTLS(addr string, cert string, key string) error {
|
||||||
|
|
||||||
|
@ -42,6 +88,18 @@ func TestCreateEngine(t *testing.T) {
|
||||||
// SetMode(TestMode)
|
// SetMode(TestMode)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
func TestLoadHTMLFiles(t *testing.T) {
|
||||||
|
td := setupHTMLFiles(t)
|
||||||
|
res, err := http.Get("http://127.0.0.1:8888/test")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := ioutil.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
|
||||||
|
td()
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadHTMLReleaseMode(t *testing.T) {
|
func TestLoadHTMLReleaseMode(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,24 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
Delims struct {
|
||||||
|
Left string
|
||||||
|
Right string
|
||||||
|
}
|
||||||
|
|
||||||
HTMLRender interface {
|
HTMLRender interface {
|
||||||
Instance(string, interface{}) Render
|
Instance(string, interface{}) Render
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLProduction struct {
|
HTMLProduction struct {
|
||||||
Template *template.Template
|
Template *template.Template
|
||||||
|
Delims Delims
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLDebug struct {
|
HTMLDebug struct {
|
||||||
Files []string
|
Files []string
|
||||||
Glob string
|
Glob string
|
||||||
|
Delims Delims
|
||||||
}
|
}
|
||||||
|
|
||||||
HTML struct {
|
HTML struct {
|
||||||
|
@ -49,10 +56,10 @@ func (r HTMLDebug) Instance(name string, data interface{}) Render {
|
||||||
}
|
}
|
||||||
func (r HTMLDebug) loadTemplate() *template.Template {
|
func (r HTMLDebug) loadTemplate() *template.Template {
|
||||||
if len(r.Files) > 0 {
|
if len(r.Files) > 0 {
|
||||||
return template.Must(template.ParseFiles(r.Files...))
|
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).ParseFiles(r.Files...))
|
||||||
}
|
}
|
||||||
if len(r.Glob) > 0 {
|
if len(r.Glob) > 0 {
|
||||||
return template.Must(template.ParseGlob(r.Glob))
|
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).ParseGlob(r.Glob))
|
||||||
}
|
}
|
||||||
panic("the HTML debug render was created without files or glob pattern")
|
panic("the HTML debug render was created without files or glob pattern")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue