foolin / gin-template Goto Github PK
View Code? Open in Web Editor NEWgolang template for gin framework!
License: MIT License
golang template for gin framework!
License: MIT License
in example gorice:
remove generated rice-box.go (considered as development mode)
go build
./gorice
get panic when acess the http index:
could not locate box "views"
happends where to call gorice.New:
gorice.New(rice.MustFindBox("views"))
alt it works when as static assets: r.StaticFS("/assets", rice.MustFindBox("views").HTTPBox())
The template like this:
how can I submit this form with put method?I try in exaples/multiple
go run main.go
and get panic:
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
[GIN-debug] GET / --> main.main.func2 (3 handlers)
[GIN-debug] GET /admin/ --> main.main.func4 (4 handlers)
[GIN-debug] Listening and serving HTTP on :9090
�[31m2018/04/19 19:39:32 [Recovery] panic recovered:
GET / HTTP/1.1
Host: 127.0.0.1:9090
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Cache-Control: max-age=0
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0
runtime error: invalid memory address or nil pointer dereference
C:/Go/src/runtime/panic.go:491 (0x42c290)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
C:/Go/src/runtime/panic.go:63 (0x42b114)
panicmem: panic(memoryError)
C:/Go/src/runtime/signal_windows.go:161 (0x4405a2)
sigpanic: panicmem()
D:/src/golang/src/github.com/foolin/gin-template/template.go:175 (0x777860)
(*TemplateEngine).executeTemplate: data, err = e.fileHandler(e.config, v)
D:/src/golang/src/github.com/foolin/gin-template/template.go:131 (0x776e75)
(*TemplateEngine).executeRender: return e.executeTemplate(out, name, data, useMaster)
D:/src/golang/src/github.com/foolin/gin-template/template.go:212 (0x77856b)
TemplateRender.Render: return r.Engine.executeRender(w, r.Name, r.Data)
:1 (0x778dc6)
D:/src/golang/src/github.com/gin-gonic/gin/context.go:427 (0x767048)
(*Context).Render: if err := r.Render(c.Writer); err != nil {
D:/src/golang/src/github.com/gin-gonic/gin/context.go:437 (0x767126)
(*Context).HTML: c.Render(code, instance)
D:/src/golang/src/github.com/foolin/gin-template/template.go:97 (0x77693f)
HTML: ctx.HTML(code, name, data)
D:/src/golang/src/gin-template/examples/multiple/main.go:40 (0x7797a0)
main.func2: gintemplate.HTML(ctx, http.StatusOK, "index", gin.H{
D:/src/golang/src/github.com/gin-gonic/gin/context.go:97 (0x765759)
(*Context).Next: c.handlersc.index
D:/src/golang/src/github.com/gin-gonic/gin/recovery.go:45 (0x774760)
RecoveryWithWriter.func1: c.Next()
D:/src/golang/src/github.com/gin-gonic/gin/context.go:97 (0x765759)
(*Context).Next: c.handlersc.index
D:/src/golang/src/github.com/gin-gonic/gin/logger.go:72 (0x773b67)
LoggerWithWriter.func1: c.Next()
D:/src/golang/src/github.com/gin-gonic/gin/context.go:97 (0x765759)
(*Context).Next: c.handlersc.index
D:/src/golang/src/github.com/gin-gonic/gin/gin.go:284 (0x76b7fa)
(*Engine).handleHTTPRequest: context.Next()
D:/src/golang/src/github.com/gin-gonic/gin/gin.go:265 (0x76b181)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
C:/Go/src/net/http/server.go:2619 (0x6b70ea)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
C:/Go/src/net/http/server.go:1801 (0x6b3183)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
C:/Go/src/runtime/asm_amd64.s:2337 (0x458090)
goexit: BYTE $0x90 // NOP
�[0m
exit status 2
In all examples i get the same. What is my mistake? I use latest gin-template from github? go 1.9.4 windosw/amd64.
Excellent work!
And I think the call method for HTML
is a bit complex in the multiple
example. There are two ways to call HTML
.
router.GET("/", func(ctx *gin.Context) {
//render with master
ctx.HTML(http.StatusOK, "index", gin.H{
"title": "Fontend title!",
})
})
backendGroup.GET("/", func(ctx *gin.Context) {
//render backend
backendRender.HTML(ctx, http.StatusOK, "index", gin.H{
"title": "Backend title!",
})
})
If possible, some helper functions can solve this, for instance...
package gintemplate
const engineKey = "github.com/foolin/gin-template/templateEngine"
func Middleware(e *TemplateEngine) gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.Set(engineKey, e)
}
}
func HTML(ctx *gin.Context, code int, name string, data interface{}) {
// If found the engine in context, use that.
if val, ok := ctx.Get(engineKey); ok {
if r, ok := val.(*TemplateEngine); ok {
r.HTML(ctx, code, name, data)
return
}
}
// If not, fallback to the default one.
ctx.HTML(code, name, data)
}
package someApp
router.GET("/", func(ctx *gin.Context) {
// the same way to use HTML
gintempalte.HTML(ctx, http.StatusOK, "index", gin.H{})
})
backendGroup := router.Group("/admin", gintemplate.Middleware(backendRender))
backendGroup.GET("/", func(ctx *gin.Context) {
// the same way to use HTML
gintemplate.HTML(ctx, http.StatusOK, "index", gin.H{})
})
How about this? If you like, I can PR.
Is there any way to build link or form with method delete
? Maybe it is possible somehow customize link with JS to send request with redirect on user's page.
Hi
i have a login page and dont want use master page but other pages can be use master page.
i set .HTMLRender = gintemplate.New(gintemplate.TemplateConfig{
Root: "views",
Extension: ".html",
Master: "layouts/master",
Partials: []string{},
Funcs: template.FuncMap{
"copy": func() string {
return time.Now().Format("2018")
},
},
DisableCache: isDebugMode,
})
but my application start with login page set empty temlate config but give an error. it wants to use master page can you help me ?
emptyMasterPage := gintemplate.NewMiddleware(gintemplate.TemplateConfig{
Root: "views",
Extension: ".html",
Master: "",
Partials: []string{},
DisableCache: true,
})
server.Engine().GET("/", func(ctx *gin.Context) {
// With the middleware, `HTML()` can detect the valid TemplateEngine.
gintemplate.HTML(ctx, http.StatusOK, "account/login", gin.H{
"title": "Backend title!",
})
}, server.EmptyMasterPage())
since go 1.10 a html page template fails with this error
a quick fix could be
diff --git a/template.go b/template.go
index 9295da9..5b93902 100644
--- a/template.go
+++ b/template.go
@@ -171,6 +171,7 @@ func (e *TemplateEngine) executeTemplate(out io.Writer, name string, data interf
if err != nil {
return fmt.Errorf("TemplateEngine render parser name:%v, path:%v, error: %v", v, path, err)
}
+ tpl = t
}
e.tplMutex.Lock()
e.tplMap[name] = tpl
but I don't make more tests for other cases
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.