diff --git a/fixtures/basic/hello.tmpl b/fixtures/basic/hello.tmpl
new file mode 100644
index 00000000..0767ef3f
--- /dev/null
+++ b/fixtures/basic/hello.tmpl
@@ -0,0 +1 @@
+
Hello {[{.name}]}
\ No newline at end of file
diff --git a/gin.go b/gin.go
index 6a8e193c..dcca2b3d 100644
--- a/gin.go
+++ b/gin.go
@@ -45,6 +45,7 @@ type (
// Create an instance of Engine, by using New() or Default()
Engine struct {
RouterGroup
+ delims render.Delims
HTMLRender render.HTMLRender
allNoRoute HandlersChain
allNoMethod HandlersChain
@@ -119,6 +120,7 @@ func New() *Engine {
UseRawPath: false,
UnescapePathValues: true,
trees: make(methodTrees, 0, 9),
+ delims: render.Delims{"{{", "}}"},
}
engine.RouterGroup.engine = engine
engine.pool.New = func() interface{} {
@@ -138,21 +140,26 @@ func (engine *Engine) allocateContext() *Context {
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) {
if IsDebugging() {
- debugPrintLoadTemplate(template.Must(template.ParseGlob(pattern)))
- engine.HTMLRender = render.HTMLDebug{Glob: pattern}
+ debugPrintLoadTemplate(template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).ParseGlob(pattern)))
+ engine.HTMLRender = render.HTMLDebug{Glob: pattern, Delims: engine.delims}
} else {
- templ := template.Must(template.ParseGlob(pattern))
+ templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).ParseGlob(pattern))
engine.SetHTMLTemplate(templ)
}
}
func (engine *Engine) LoadHTMLFiles(files ...string) {
if IsDebugging() {
- engine.HTMLRender = render.HTMLDebug{Files: files}
+ engine.HTMLRender = render.HTMLDebug{Files: files, Delims: engine.delims}
} else {
- templ := template.Must(template.ParseFiles(files...))
+ templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).ParseFiles(files...))
engine.SetHTMLTemplate(templ)
}
}
@@ -161,6 +168,7 @@ func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
if len(engine.trees) > 0 {
debugPrintWARNINGSetHTMLTemplate()
}
+
engine.HTMLRender = render.HTMLProduction{Template: templ}
}
diff --git a/gin_test.go b/gin_test.go
index cc24bc92..3ab46976 100644
--- a/gin_test.go
+++ b/gin_test.go
@@ -5,14 +5,60 @@
package gin
import (
+ "fmt"
+ "io/ioutil"
+ "net/http"
"reflect"
"testing"
+ "time"
"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
-// 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, "Hello world
", string(resp[:]))
+
+ td()
+}
+
// func (engine *Engine) LoadHTMLFiles(files ...string) {
// func (engine *Engine) RunTLS(addr string, cert string, key string) error {
@@ -42,6 +88,18 @@ func TestCreateEngine(t *testing.T) {
// 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, "Hello world
", string(resp[:]))
+ td()
+}
+
func TestLoadHTMLReleaseMode(t *testing.T) {
}
diff --git a/render/html.go b/render/html.go
index a3cbda67..3ea5e08e 100644
--- a/render/html.go
+++ b/render/html.go
@@ -10,17 +10,24 @@ import (
)
type (
+ Delims struct {
+ Left string
+ Right string
+ }
+
HTMLRender interface {
Instance(string, interface{}) Render
}
HTMLProduction struct {
Template *template.Template
+ Delims Delims
}
HTMLDebug struct {
- Files []string
- Glob string
+ Files []string
+ Glob string
+ Delims Delims
}
HTML struct {
@@ -49,10 +56,10 @@ func (r HTMLDebug) Instance(name string, data interface{}) Render {
}
func (r HTMLDebug) loadTemplate() *template.Template {
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 {
- 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")
}