views

package
v0.0.0-...-be230c0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2023 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AlertLvlDefault is used to render the default alert style.
	AlertLvlDefault = ""
	// AlertLvlPrimary is used to render an alert with our primary color.
	AlertLvlPrimary = "primary"
	// AlertLvlInfo is used to render an alert with general information.
	AlertLvlInfo = "info"
	// AlertLvlSuccess is used to render an alert with a success message.
	AlertLvlSuccess = "success"
	// AlertLvlWarning is used to render an alert with a warning message.
	AlertLvlWarning = "warning"
	// AlertLvlError is used to render an alert with an error message.
	AlertLvlError = "danger"
	// AlertLvlDark is used to render a dark-style alert.
	AlertLvlDark = "dark"
	// AlertLvlLink is used to render an alert with our link color.
	AlertLvlLink = "link"

	// AlertMsgErrGeneric is displayed when any arbitrary error is encountered by the backend.
	AlertMsgErrGeneric = "Something went wrong. Please try again, and contact us if the problem persists."
)
View Source
const (
	// LayoutsDir specifies where layout templates are stored.
	LayoutsDir string = "views/layouts/"
	// PartialsDir specifies where partials (small, reusable templates) are stored.
	PartialsDir string = "views/layouts/partials/"
	// TemplateDir specifies where any template might be found.
	TemplateDir string = "views/"
	// TemplateExt specifies the extension of our templates.
	TemplateExt string = ".gohtml"
)

Variables

This section is empty.

Functions

func RedirectAlert

func RedirectAlert(w http.ResponseWriter, r *http.Request, url string, code int, alert Alert)

RedirectAlert performs an http.Redirect after persisting the provided alert in a cookie so that it can be displayed when the new page is loaded.

Types

type Alert

type Alert struct {
	// Level can be one of the following: AlertLvlDefault, AlertLvlDark, AlertLvlPrimary, AlertLvlLink, AlertLvlInfo,
	// AlertLvlSuccess, AlertLvlWarning, or AlertLvlError.
	Level string
	// Message is the message which will be rendered in the alert.
	Message string
}

Alert is used to render the alert template.

func NewAlert

func NewAlert(lvl, msg string) *Alert

NewAlert returns an *Alert with the specified lvl and msg. For error alerts, use NewAlertErr instead.

func NewAlertErr

func NewAlertErr(err error) *Alert

NewAlertErr returns an *Alert with the AlertLvlError level. If err is a PublicError, then the corresponding public error message will be the alert's message; otherwise, the alert's message will be AlertMsgErrGeneric.

type Data

type Data struct {
	Alert *Alert
	User  *models.User
	Yield interface{}
}

Data is the top-level structure that views expect data to come in. Data intended for the yield template must be in the Yield field.

func (*Data) SetAlert

func (d *Data) SetAlert(lvl, msg string)

SetAlert creates an Alert with the specified lvl and msg and sets it to d.Alert. For error alerts, use SetAlertErr instead.

func (*Data) SetAlertErr

func (d *Data) SetAlertErr(err error)

SetAlertErr creates an Alert with the AlertLvlError level and sets it to d.Alert. If the error is a PublicError, then the corresponding public error message will be the alert's message; otherwise, the alert's message will be AlertMsgErrGeneric.

type PublicError

type PublicError interface {
	error

	// Public returns an error message which is acceptable for display to the user.
	// The message should not reveal internal system design to the user, should start with a capital letter,
	// and end with a period.
	Public() string
}

PublicError defines an error type which can be safely displayed to the user.

type View

type View struct {
	Template *template.Template
	Layout   string
}

View includes a template and layout file to be used to render views.

func NewView

func NewView(layout string, files ...string) *View

NewView is used to make new Views out of a list of filenames.

func (*View) Render

func (v *View) Render(w http.ResponseWriter, r *http.Request, data interface{})

Render executes a template and renders the provided data in it. If data is not of views.Data type, Render moves the provided data to the Yield field of a Data struct so that users of Render need not create a Data struct. It uses a buffer to ensure complete writing of the executed template data to ResponseWriter.

func (*View) ServeHTTP

func (v *View) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL