Make Session available in the templates

This commit is contained in:
Noah 2018-04-27 14:49:21 -07:00
parent efeb2e934e
commit a1c84fa1e9

View File

@ -7,6 +7,7 @@ import (
"strings"
"time"
gorilla "github.com/gorilla/sessions"
"github.com/kirsle/blog/internal/log"
"github.com/kirsle/blog/internal/middleware"
"github.com/kirsle/blog/internal/middleware/auth"
@ -35,6 +36,7 @@ type vars struct {
RequestDuration time.Duration
// Common template variables.
Session *gorilla.Session
Message string
Flashes []string
Error error
@ -77,6 +79,7 @@ func Template(w io.Writer, r *http.Request, path string, data interface{}) error
if rw, ok := w.(http.ResponseWriter); ok {
rw.Header().Set("Content-Type", "text/html; encoding=UTF-8")
session := sessions.Get(r)
v.Session = session
// Flashed messages.
if flashes := session.Flashes(); len(flashes) > 0 {