Documentation ¶
Overview ¶
Package template renders templates using github.com/yossi/ace, providing text translation through github.com/wscherphof/msg.
It provides an httprouter.Handle to just render a template as the action for a route.
It renders and sends email messages.
It renders handled responses to GET requests.
It renders errors from request handlers.
It implements the Post-Redirect-Get pattern, redirecting to a GET request, rendering the template with data from a POST, PUT, or DELETE request
Index ¶
- Variables
- func Error(w http.ResponseWriter, r *http.Request, err error, conflict bool)
- func ErrorTail(w http.ResponseWriter, r *http.Request, err error, conflict bool, ...)
- func Handle(dir, base, inner string, opt_data ...map[string]interface{}) httprouter.Handle
- func String(r *http.Request, dir, base, inner string, opt_data ...map[string]interface{}) string
- type BaseType
- type EmailType
- type GetType
- type PRGType
Constants ¶
This section is empty.
Variables ¶
var ErrInternalServerError = errors.New("ErrInternalServerError")
Functions ¶
func Error ¶
Error renders a generic template with details about the given error.
If conflict is true, the status code is http.StatusConflict. Otherwise, it's http.StatusInternalServerError.
func ErrorTail ¶
func ErrorTail(w http.ResponseWriter, r *http.Request, err error, conflict bool, dir, tail string, opt_data ...map[string]interface{})
ErrorTail is like Error, but tails the generic error template with another, as an "inner" template.
The tail template should start with:
= content tail
Any data is passed to the tail template.
func Handle ¶
func Handle(dir, base, inner string, opt_data ...map[string]interface{}) httprouter.Handle
Handle returns a Handle executing a template
func String ¶
String loads and executes a template, returning the output.
dir is a directory (one deep) under /resources/templates
base is the template name (filename without extension) in dir
inner is the inner template name (without extension) in dir
Both base and inner may include paths relative to dir.
data is the pipeline data to pass to the template
Types ¶
type EmailType ¶
type EmailType struct {
*BaseType
}
type GetType ¶
type GetType struct {
*BaseType
}
type PRGType ¶
type PRGType struct {
*BaseType
}
func PRG ¶
func PRG(w http.ResponseWriter, r *http.Request, dir, base string, opt_inner ...string) (prg *PRGType)
PRG implments the Post-Redirect-Get pattern.
If the request's method is GET, it returns nil, and runs the template, populating it with the data stored in the POST processing.
Otherwise, it returns a type that, just as net/url.Values, listens to Set(key, value) to register string data. Call Run() to execute the redirect. Register a GET route for the redirect with the same path, suffixed with /<method>
Example:
router.POST("/account", ratelimit.Handle(account.NewAccount)) router.GET("/account/post", account.NewAccount)
and
func NewAccount(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if t := template.PRG(w, r, "account", "NewAccount"); t == nil { return // GET template gets the id parameter } else if account, err := model.NewAccount(...); err != nil { template.Error(...) } else { t.Set("id", account.ID) t.Run() // Redirect } }
Works with POST, as well as with PUT, PATCH, and DELETE.