mirror of
https://github.com/foolin/goview.git
synced 2025-10-07 07:40:56 +08:00
Initial commit
This commit is contained in:
73
_examples/gin-multiple/main.go
Normal file
73
_examples/gin-multiple/main.go
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2018 Foolin. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/foolin/goview"
|
||||
"github.com/foolin/goview/supports/ginview"
|
||||
"github.com/gin-gonic/gin"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
|
||||
//new template engine
|
||||
router.HTMLRender = ginview.New(goview.Config{
|
||||
Root: "views/frontend",
|
||||
Extension: ".html",
|
||||
Master: "layouts/master",
|
||||
Partials: []string{"partials/ad"},
|
||||
Funcs: template.FuncMap{
|
||||
"copy": func() string {
|
||||
return time.Now().Format("2006")
|
||||
},
|
||||
},
|
||||
DisableCache: true,
|
||||
})
|
||||
|
||||
router.GET("/", func(ctx *gin.Context) {
|
||||
// `HTML()` is a helper func to deal with multiple TemplateEngine's.
|
||||
// It detects the suitable TemplateEngine for each path automatically.
|
||||
ginview.HTML(ctx, http.StatusOK, "index", gin.H{
|
||||
"title": "Frontend title!",
|
||||
})
|
||||
})
|
||||
|
||||
//=========== Backend ===========//
|
||||
|
||||
//new middleware
|
||||
mw := ginview.NewMiddleware(goview.Config{
|
||||
Root: "views/backend",
|
||||
Extension: ".html",
|
||||
Master: "layouts/master",
|
||||
Partials: []string{},
|
||||
Funcs: template.FuncMap{
|
||||
"copy": func() string {
|
||||
return time.Now().Format("2006")
|
||||
},
|
||||
},
|
||||
DisableCache: true,
|
||||
})
|
||||
|
||||
// You should use helper func `Middleware()` to set the supplied
|
||||
// TemplateEngine and make `HTML()` work validly.
|
||||
backendGroup := router.Group("/admin", mw)
|
||||
|
||||
backendGroup.GET("/", func(ctx *gin.Context) {
|
||||
// With the middleware, `HTML()` can detect the valid TemplateEngine.
|
||||
ginview.HTML(ctx, http.StatusOK, "index", gin.H{
|
||||
"title": "Backend title!",
|
||||
})
|
||||
})
|
||||
|
||||
router.Run(":9090")
|
||||
}
|
Reference in New Issue
Block a user