我使用 Gorilla 會話(通過 negroni-sessions)將我的用戶會話存儲在 cookie 中。我也github.com/unrolled/render用于我的 HTML 模板渲染:main.go: package main import ( ... "github.com/codegangsta/negroni" "github.com/goincremental/negroni-sessions" "github.com/goincremental/negroni-sessions/cookiestore" "github.com/julienschmidt/httprouter" "github.com/unrolled/render" ... ) func init() { ... ren = render.New(render.Options{ Directory: "templates", Layout: "layout", Extensions: []string{".html"}, Funcs: []template.FuncMap{TemplateHelpers}, IsDevelopment: false, }) ... } func main() { ... router := httprouter.New() router.GET("/", HomeHandler) // Add session store store := cookiestore.New([]byte("my password")) store.Options(sessions.Options{ //MaxAge: 1200, Domain: "", Path: "/", }) n := negroni.New( negroni.NewRecovery(), sessions.Sessions("cssession", store), negroni.NewStatic(http.Dir("../static")), ) n.UseHandler(router) n.Run(":9000") }正如你在上面看到的,我使用了一個layout.html主 HTML 模板,它包含在任何頁面呈現時,比如我的主頁: package main import ( "html/template" "github.com/julienschmidt/httprouter" ) func HomeHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) { var model = struct { CatalogPicks []PromotionalModelList ClearanceItems []Model }{ CatalogPicks: GetCatalogPicks(), ClearanceItems: GetClearanceItems(), } ren.HTML(w, http.StatusOK, "home", model) }在我的layout.html主 HTML 模板中,我想呈現一個管理菜單,但前提是當前用戶是管理員:布局.html:<!doctype html><html> <head> <meta charset="utf-8"> <title>{{ template "title" . }}</title> ... </head> <body>
- 2 回答
- 0 關注
- 279 瀏覽
添加回答
舉報
0/150
提交
取消