This repository was archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.go
More file actions
81 lines (69 loc) · 1.84 KB
/
page.go
File metadata and controls
81 lines (69 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// This file holds template attrs & methods
package mulate
import (
"github.com/pkg/errors"
"html/template"
"net/http"
)
// ErrRedirect is an error returned when page needs to be redirected
var ErrRedirect = errors.New("Abort with redirect")
// Page holds page attributes
type Page struct {
Title string
Status int
ContentType string
Layout string
JS []string
CSS []string
Request *http.Request
content template.HTML
funcs template.FuncMap
errLayout string
uri string
}
// URI returns GIN page uri
func (p *Page) URI() (string, error) {
return p.uri, nil
}
// SetLayout - set page layout
func (p *Page) SetLayout(name string) (string, error) {
p.Layout = name
return "", nil
}
// SetTitle - set page title
func (p *Page) SetTitle(name string) (string, error) {
p.Title = name
return "", nil
}
// AddJS - add .js file to scripts list
func (p *Page) AddJS(file string) (string, error) {
p.JS = append(p.JS, file)
return "", nil
}
// AddCSS - add .css file to styles list
func (p *Page) AddCSS(file string) (string, error) {
p.JS = append(p.CSS, file)
return "", nil
}
// SetContentType - set page content type
func (p *Page) SetContentType(name string) (string, error) {
p.ContentType = name
return "", nil
}
// Raise - abort template processing (if given) and raise error
func (p *Page) Raise(status int, title, message string, abort bool) (string, error) {
p.Status = status
p.Title = title
p.content = template.HTML(message)
p.Layout = p.errLayout
if abort {
return "", errors.New(message)
}
return "", nil
}
// RedirectFound - abort template processing and return redirect with StatusFound status
func (p *Page) RedirectFound(uri string) (string, error) {
p.Status = http.StatusFound
p.Title = uri
return "", ErrRedirect // TODO: Is there a way to pass status & title via error?
}