A web blog and personal homepage engine written in Go.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.1 KiB

package responses
import (
// NewTemplateVars creates the TemplateVars for your current request.
func NewTemplateVars(w io.Writer, r *http.Request) TemplateValues {
var s = settings.Current
user, _ := authentication.CurrentUser(r)
ses := session.Get(r)
v := TemplateValues{
SetupNeeded: !s.Initialized,
Title: s.Title,
Description: s.Description,
Request: r,
RequestTime: time.Now(),
RequestDuration: time.Duration(0),
Session: ses,
Path: r.URL.Path,
LoggedIn: authentication.LoggedIn(r),
IsAdmin: user.IsAdmin,
CurrentUser: user,
V: map[string]interface{}{},
if rw, ok := w.(http.ResponseWriter); ok {
v.ResponseWriter = rw
v.Flashes = session.GetFlashes(rw, r)
return v
// TemplateValues holds the context for html templates.
type TemplateValues struct {
// When the site needs the initial config.
SetupNeeded bool
// Config values available as template variables.
Title string
Description string
// Request variables
Request *http.Request
RequestTime time.Time
RequestDuration time.Duration
ResponseWriter http.ResponseWriter
FormValues url.Values
Path string // request path
TemplatePath string // file path of html template, like "_builtin/error.gohtml"
// Session variables
Session *sessions.Session
LoggedIn bool
IsAdmin bool
CurrentUser models.User
// Common template variables.
Message string
Error interface{}
ValidationError map[string][]string // form validation errors
Flashes []string
// Arbitrary controller-specific fields go in V.
V map[string]interface{}
// Flash adds a message to flash on the next template render.
func (v *TemplateValues) Flash(msg string, args ...interface{}) {
v.Flashes = append(v.Flashes, fmt.Sprintf(msg, args...))