-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_duplicate_resources_test.go
More file actions
180 lines (156 loc) · 4.83 KB
/
Copy pathexample_duplicate_resources_test.go
File metadata and controls
180 lines (156 loc) · 4.83 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package temple_test
import (
"context"
"log/slog"
"os"
"impractical.co/temple"
)
type DuplicateResourcesSite struct {
// anonymously embedding a *CachedSite makes MySite a Site implementation
*temple.CachedSite
// a configurable title for our site
Title string
}
type DuplicateResourcesHomePage struct {
Name string
Layout DuplicateResourcesLayout
}
func (DuplicateResourcesHomePage) Templates(_ context.Context) []string {
return []string{"home.html.tmpl"}
}
func (page DuplicateResourcesHomePage) UseComponents(_ context.Context) []temple.Component {
return []temple.Component{
page.Layout,
}
}
func (DuplicateResourcesHomePage) Key(_ context.Context) string {
return "home.html.tmpl"
}
func (page DuplicateResourcesHomePage) ExecutedTemplate(_ context.Context) string {
return page.Layout.BaseTemplate()
}
func (DuplicateResourcesHomePage) LinkCSS(_ context.Context) []temple.CSSLink {
return []temple.CSSLink{
{Href: "https://example.com/b.css", Type: "text/css", Rel: "stylesheet"},
{Href: "https://example.com/c.css", Type: "text/css", Rel: "stylesheet"},
}
}
func (DuplicateResourcesHomePage) EmbedCSS(_ context.Context) []temple.CSSInline {
return []temple.CSSInline{
{TemplatePath: "b.inline.css"},
{TemplatePath: "c.inline.css"},
}
}
func (DuplicateResourcesHomePage) LinkJS(_ context.Context) []temple.JSLink {
return []temple.JSLink{
{Src: "https://example.com/b.js", Type: "text/javascript"},
{Src: "https://example.com/c.js", Type: "text/javascript"},
}
}
func (DuplicateResourcesHomePage) EmbedJS(_ context.Context) []temple.JSInline {
return []temple.JSInline{
{TemplatePath: "b.inline.js"},
{TemplatePath: "c.inline.js"},
}
}
type DuplicateResourcesLayout struct {
}
func (layout DuplicateResourcesLayout) Templates(_ context.Context) []string {
return []string{layout.BaseTemplate()}
}
func (DuplicateResourcesLayout) BaseTemplate() string {
return "base.html.tmpl"
}
func (DuplicateResourcesLayout) LinkCSS(_ context.Context) []temple.CSSLink {
return []temple.CSSLink{
{Href: "https://example.com/a.css", Type: "text/css", Rel: "stylesheet"},
{Href: "https://example.com/b.css", Type: "text/css", Rel: "stylesheet"},
}
}
func (DuplicateResourcesLayout) EmbedCSS(_ context.Context) []temple.CSSInline {
return []temple.CSSInline{
{TemplatePath: "a.inline.css"},
{TemplatePath: "b.inline.css"},
}
}
func (DuplicateResourcesLayout) LinkJS(_ context.Context) []temple.JSLink {
return []temple.JSLink{
{Src: "https://example.com/a.js", Type: "text/javascript"},
{Src: "https://example.com/b.js", Type: "text/javascript"},
}
}
func (DuplicateResourcesLayout) EmbedJS(_ context.Context) []temple.JSInline {
return []temple.JSInline{
{TemplatePath: "a.inline.js"},
{TemplatePath: "b.inline.js"},
}
}
func ExampleRender_duplicateResources() {
// normally you'd use something like embed.FS or os.DirFS for this
// for example purposes, we're just hardcoding values
var templates = staticFS{
"home.html.tmpl": `{{ define "body" }}Hello, {{ .Page.Name }}. This is my home page.{{ end }}`,
"a.inline.css": `body { color: red; }`,
"b.inline.css": `html { background-color: black; }`,
"c.inline.css": `a { color: yellow; }`,
"a.inline.js": `alert('a!');`,
"b.inline.js": `alert('b!');`,
"c.inline.js": `alert('c!');`,
"base.html.tmpl": `
<!doctype html>
<html lang="en">
<head>
<title>{{ .Site.Title }}</title>
{{- .CSS -}}
{{- .HeaderJS -}}
</head>
<body>
{{ block "body" . }}{{ end }}
{{- .FooterJS -}}
</body>
</html>`,
}
// usually the context comes from the request, but here we're building it from scratch and adding a logger
ctx := temple.LoggingContext(context.Background(), slog.Default())
site := DuplicateResourcesSite{
CachedSite: temple.NewCachedSite(templates),
Title: "My Example Site",
}
page := DuplicateResourcesHomePage{
Name: "Visitor",
Layout: DuplicateResourcesLayout{},
}
temple.Render(ctx, os.Stdout, site, page)
//Output:
// <!doctype html>
// <html lang="en">
// <head>
// <title>My Example Site</title><link href="https://example.com/a.css" rel="stylesheet" type="text/css">
// <link href="https://example.com/b.css" rel="stylesheet" type="text/css">
// <link href="https://example.com/c.css" rel="stylesheet" type="text/css">
// <style>
// body { color: red; }
// </style>
// <style>
// html { background-color: black; }
// </style>
// <style>
// a { color: yellow; }
// </style>
// <script type="text/javascript" src="https://example.com/a.js"></script>
// <script type="text/javascript" src="https://example.com/b.js"></script>
// <script type="text/javascript" src="https://example.com/c.js"></script>
// <script>
// alert('a!');
// </script>
// <script>
// alert('b!');
// </script>
// <script>
// alert('c!');
// </script>
// </head>
// <body>
// Hello, Visitor. This is my home page.</body>
// </html>
}