forked from razonyang/clevergo-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.go
68 lines (56 loc) · 1.13 KB
/
alert.go
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
package bootstrap
import (
"bytes"
"encoding/gob"
"html/template"
)
func init() {
gob.Register(Alert{})
}
var alertTmpl = template.Must(template.New("alert").Parse(`
<div class="alert alert-{{ .Style }}" role="alert">
{{ .Text }}
</div>`))
// Alert styles.
const (
AlertStyleWarning = "warning"
AlertStyleSuccess = "success"
AlertStyleDanger = "danger"
AlertStyleInfo = "info"
)
type Alert struct {
Style string
Text string
}
func NewAlert(text, style string) Alert {
return Alert{
Style: style,
Text: text,
}
}
func NewSuccessAlert(text string) Alert {
alert := NewAlert(text, AlertStyleSuccess)
return alert
}
func NewDangerAlert(text string) Alert {
alert := NewAlert(text, AlertStyleDanger)
return alert
}
func NewWarningAlert(text string) Alert {
alert := NewAlert(text, AlertStyleWarning)
return alert
}
func NewInfoAlert(text string) Alert {
alert := NewAlert(text, AlertStyleInfo)
return alert
}
func (a Alert) Message() string {
w := &bytes.Buffer{}
if err := alertTmpl.Execute(w, a); err != nil {
return err.Error()
}
return w.String()
}
func (a Alert) Output() string {
return a.Message()
}