Documentation ¶
Overview ¶
Package context is used to create a context that can be passed to the execution of templates. And helps with conveying information like the logged in user to the user interface.
A new context can be created using the New() function.
c := context.New()
To add information to a context use the With() function. It can be easily chained with the New() method.
c := context.New().With("key", "value").With("number", 9)
The package also provides a method with which user information is added to the context automatically. To be able to use this the packages need to get a session handler via its Init() function.
context.Init(mySessionHandler) c := context.New().WithUserInformation(myRequest)
Index ¶
Examples ¶
Constants ¶
const ( LoggedInKey = "LoggedIn" UserKey = "User" )
Reserved keys for user information
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context map[string]interface{}
Context contains information useful for the execution of templates
func (Context) With ¶
With is a convince method used to chain-add content to the context. Values can only be added once. If a value with given key already exists this function will panic. If 'nil' is passed as data it will not be stored. This means this method can't be used to delete values.
Example ¶
ExampleContext_With is an example for the usage of the context.Context.With() method.
c := New().With("key", "value").With("int", 5) c.With("struct", struct { a int b string }{9, "bla"}) fmt.Println(c["key"], c["int"], c["struct"])
Output: value 5 {9 bla}
func (Context) WithUserInformation ¶
WithUserInformation fills the context with information about the currently logged in user. This call can be chained. Be careful not to set values with the keys used by this method for the user information. (See the LoggedInKey and UserKey constants of this package) If With() was used before to add values with said key this method will panic.