/
helpers.go
78 lines (66 loc) · 2.04 KB
/
helpers.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
69
70
71
72
73
74
75
76
77
78
package assets
import (
"fmt"
"html/template"
"strings"
)
const (
styleTemplate = `<link href="/assets/styles/%s" media="all" rel="stylesheet" type="text/css" />`
scriptTemplate = `<script src="/assets/scripts/%s" type="text/javascript" ></script>`
)
// StyleLink converts a set of group names to one style link tag (production) or to a list of style link tags (development)
func (c *Collection) StyleLink(names ...string) template.HTML {
var html template.HTML
// Iterate through names, setting up links for each
// we link to groups if we have them, else we fall back to normal links
for _, name := range names {
g := c.Group(name)
if g.stylehash != "" {
if c.serveCompiled {
html = html + StyleLink(g.StyleName())
} else {
for _, f := range g.Styles() {
html = html + StyleLink(f.name) + template.HTML("\n")
}
}
} else {
html = html + StyleLink(name)
}
}
return html
}
// ScriptLink converts a set of group names to one script tag (production) or to a list of script tags (development)
func (c *Collection) ScriptLink(names ...string) template.HTML {
var html template.HTML
// Iterate through names, setting up links for each
// we link to groups if we have them, else we fall back to normal links
for _, name := range names {
g := c.Group(name)
if g.stylehash != "" {
if c.serveCompiled {
html = html + ScriptLink(g.ScriptName())
} else {
for _, f := range g.Scripts() {
html = html + ScriptLink(f.name) + template.HTML("\n")
}
}
} else {
html = html + ScriptLink(name)
}
}
return html
}
// StyleLink returns an html tag for a given file path
func StyleLink(name string) template.HTML {
if !strings.HasSuffix(name, ".css") {
name = name + ".css"
}
return template.HTML(fmt.Sprintf(styleTemplate, template.URLQueryEscaper(name)))
}
// ScriptLink returns an html tag for a given file path
func ScriptLink(name string) template.HTML {
if !strings.HasSuffix(name, ".js") {
name = name + ".js"
}
return template.HTML(fmt.Sprintf(scriptTemplate, template.URLQueryEscaper(name)))
}