I'm curious as to how I would set a template variable from a piece of middleware I have, this is my middleware:
func IsUserLoggedIn(router http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("Checking if user is logged in")
session, err := store.Get(r, "loggedIn")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// Perform some If Statements and set True/False
// Set Session Variables
session.Values["isLoggedIn"] = true
// Save Session
session.Save(r, w)
// Set Template Variable
router.ServeHTTP(w, r)
})
}
Then in my template for my main layout:
{{ define "layout" }}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/static/css/main.css">
<title> {{ .Title }} </title>
</head>
<body>
{{ SET SOMETHING HERE TO SAY YOU'RE LOGGED IN }}
{{ GET THE SESSION VARIABLE AND SET ACCORDINGLY }}
{{ template "body" .}}
</body>
</html>
{{ end }}
Basically how can I access a session variable inside my template?