Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom Delims support #860

Merged
merged 9 commits into from
May 29, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fixtures/basic/hello.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello {[{.name}]}</h1>
19 changes: 14 additions & 5 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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
Expand Down Expand Up @@ -102,6 +103,7 @@ func New() *Engine {
HandleMethodNotAllowed: false,
ForwardedByClientIP: true,
trees: make(methodTrees, 0, 9),
delims: render.Delims{"{{", "}}"},
}
engine.RouterGroup.engine = engine
engine.pool.New = func() interface{} {
Expand All @@ -121,21 +123,27 @@ 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.ParseGlob(pattern)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this comments code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shenshouer Please remove this comments.

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)
}
}
Expand All @@ -144,6 +152,7 @@ func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
if len(engine.trees) > 0 {
debugPrintWARNINGSetHTMLTemplate()
}

engine.HTMLRender = render.HTMLProduction{Template: templ}
}

Expand Down
63 changes: 62 additions & 1 deletion gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,60 @@ import (
"reflect"
"testing"

"net/http"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why all these blank lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the miss inspection, this was auto import package and format style by the vscode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the empty line for import native package.

"fmt"
"io/ioutil"

"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, "<h1>Hello world</h1>", string(resp[:]))

td()
}

// func (engine *Engine) LoadHTMLFiles(files ...string) {
// func (engine *Engine) RunTLS(addr string, cert string, key string) error {

Expand Down Expand Up @@ -42,6 +91,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, "<h1>Hello world</h1>", string(resp[:]))
td()
}

func TestLoadHTMLReleaseMode(t *testing.T) {

}
Expand Down
15 changes: 11 additions & 4 deletions render/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down