Documentation ¶
Overview ¶
Package flash provides flash message utilities
Index ¶
- Constants
- func Funcs(funcs template.FuncMap, w http.ResponseWriter, r *http.Request, key string) template.FuncMap
- type Context
- type Encoder
- func (fe Encoder) GetFlashMsgs(w http.ResponseWriter, r *http.Request) (map[string][]FlashMsg, error)
- func (fe Encoder) SetFlashMsgs(w http.ResponseWriter, r *http.Request, msgs map[string][]string) (*http.Request, error)
- func (fe Encoder) UnsetFlashMsgs(w http.ResponseWriter, r *http.Request, keys ...string) (*http.Request, error)
- func (fe Encoder) UnsetFlashMsgsHandler(keys ...string) func(http.Handler) http.Handler
- type FlashMsg
Constants ¶
const ( Success = "success" Error = "error" Warning = "warning" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Context ¶
type Context string
const (
ContextFlashMsgs Context = "ContextFlashMsgs" // map[string][]string
)
type Encoder ¶
type Encoder struct {
Key string
}
func NewEncoder ¶
func (Encoder) GetFlashMsgs ¶
func (fe Encoder) GetFlashMsgs(w http.ResponseWriter, r *http.Request) (map[string][]FlashMsg, error)
GetFlashMsgs will get all flash messages from both the cookie and request context, after which it will delete the cookie (hence a 'flash' message).
Make sure you use an Encoder with the same seceret key as the encoder that you set the flash messages with:
flashmsgs, _ := flash.NewEncoder("secret_key").GetFlashMsgs(w, r)
Usually you won't call this function directly in your handlers, instead you will include the "helpers/flash/flash.html" template in your html file which will automatically call this function and display the flash messages, if any.
func (Encoder) SetFlashMsgs ¶
func (fe Encoder) SetFlashMsgs(w http.ResponseWriter, r *http.Request, msgs map[string][]string) (*http.Request, error)
SetFlashMsgs will set a bunch of flash messages into a cookie that can be read later by GetFlashMsgs.
NOTE: This returns a new request with the flash messages in the context updated, so make sure to re-assign the request:
r, _ = flash.NewEncoder("secret_key").SetFlashMsgs(w, r, msgs)
func (Encoder) UnsetFlashMsgs ¶
func (fe Encoder) UnsetFlashMsgs(w http.ResponseWriter, r *http.Request, keys ...string) (*http.Request, error)
UnsetFlashMsgs will unset all flash messages under the specified keys. This is useful if you wish to suppress particular flash messages that you know were set in a previous middleware handler.
NOTE: This returns a new request with the flash messages in the context updated, so make sure to re-assign the request:
r, _ = flash.NewEncoder("secret_key").UnsetFlashMsgs(flash.Success)