-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcell.go
66 lines (56 loc) · 1.33 KB
/
cell.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
package tables
import (
"fmt"
"html"
"html/template"
"github.com/coscms/forms/widgets"
)
type Cells []*Cell
func (c Cells) render() string {
var r string
for _, v := range c {
r += v.render()
}
return r
}
func (c Cells) Render() template.HTML {
return template.HTML(c.render())
}
type Cell struct {
IsHead bool `json:"isHead" xml:"isHead"`
Style string `json:"style" xml:"style"`
Template string `json:"template" xml:"template"`
Attributes Attributes `json:"attributes,omitempty" xml:"attributes,omitempty"`
Content interface{} `json:"content" xml:"content"`
widget widgets.WidgetInterface
}
func (c *Cell) String() string {
return fmt.Sprint(c.Content)
}
func (c *Cell) defaultHTMLString() string {
tag := TagCell
if c.IsHead {
tag = TagHeadCell
}
return `<` + tag + GenAttr(c.Attributes) + `>` + html.EscapeString(fmt.Sprint(c.Content)) + `</` + tag + `>`
}
func (c *Cell) render() string {
if len(c.Template) == 0 {
return c.defaultHTMLString()
}
if c.widget == nil {
tag := TagCell
if c.IsHead {
tag = TagHeadCell
}
c.widget = widgets.BaseWidget(c.Style, tag, c.Template)
}
data := map[string]interface{}{
`content`: c.Content,
`attributes`: c.Attributes,
}
return c.widget.Render(data)
}
func (c *Cell) Render() template.HTML {
return template.HTML(c.render())
}