亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過會話變量在 Golang layout.tpl 中條件渲染 HTML

通過會話變量在 Golang layout.tpl 中條件渲染 HTML

Go
陪伴而非守候 2021-09-27 21:28:01
我使用 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 回答

  • 2 回答
  • 0 關注
  • 279 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號