-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_inlinejs_implicit_disabled_test.go
More file actions
146 lines (129 loc) · 4.49 KB
/
example_inlinejs_implicit_disabled_test.go
File metadata and controls
146 lines (129 loc) · 4.49 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
package temple_test
import (
"context"
"log/slog"
"os"
"impractical.co/temple"
)
type InlineJSDisableImplicitDependencySite struct {
// anonymously embedding a *CachedSite makes MySite a Site implementation
*temple.CachedSite
// a configurable title for our site
Title string
}
type InlineJSDisableImplicitDependencyHomePage struct {
Layout InlineJSDisableImplicitDependencyLayout
User string
}
func (InlineJSDisableImplicitDependencyHomePage) Templates(_ context.Context) []string {
return []string{"home.html.tmpl"}
}
func (h InlineJSDisableImplicitDependencyHomePage) UseComponents(_ context.Context) []temple.Component {
return []temple.Component{
h.Layout,
}
}
func (InlineJSDisableImplicitDependencyHomePage) Key(_ context.Context) string {
return "home.html.tmpl"
}
func (h InlineJSDisableImplicitDependencyHomePage) ExecutedTemplate(_ context.Context) string {
return h.Layout.BaseTemplate()
}
func (InlineJSDisableImplicitDependencyHomePage) EmbedJS(_ context.Context) []temple.JSInline {
return []temple.JSInline{
// because these are all declared in a single component,
// they'll have an implicit ordering when rendered. JavaScript
// resources get divided into two groups; those with
// PlaceInFooter true and those with PlaceInFooter false, then
// ordered by their constraints. So header.b.js.tmpl will
// always be rendered before header.a.js.tmpl, which will
// always be rendered before header.c.js.tmpl. Likewise,
// footer.b.js.tmpl will always be rendered before
// footer.a.js.tmpl, which will always be rendered before
// footer.c.js.tmpl.
//
// However, because DisableImplicitOrdering is set on
// header.b.js.tmpl and footer.b.js.tmpl, header.a.js.tmpl will
// have no dependency on header.b.js.tmpl, and footer.a.js.tmpl
// will have no dependency on footer.b.js.tmpl, and the normal
// lexicographical ordering will decide to put header.a.js.tmpl
// and footer.a.js.tmpl first.
{TemplatePath: "header.b.js.tmpl", DisableImplicitOrdering: true},
{TemplatePath: "footer.b.js.tmpl", PlaceInFooter: true, DisableImplicitOrdering: true},
{TemplatePath: "header.a.js.tmpl"},
{TemplatePath: "footer.a.js.tmpl", PlaceInFooter: true},
{TemplatePath: "header.c.js.tmpl"},
{TemplatePath: "footer.c.js.tmpl", PlaceInFooter: true},
}
}
type InlineJSDisableImplicitDependencyLayout struct {
}
func (b InlineJSDisableImplicitDependencyLayout) Templates(_ context.Context) []string {
return []string{b.BaseTemplate()}
}
func (InlineJSDisableImplicitDependencyLayout) BaseTemplate() string {
return "base.html.tmpl"
}
func ExampleRender_inlineJSWithDisableImplicitDependency() {
// 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.User }}. This is my home page.{{ end }}`,
"base.html.tmpl": `
<!doctype html>
<html lang="en">
<head>
<title>{{ .Site.Title }}</title>
{{- .CSS -}}
{{- .HeaderJS -}}
</head>
<body>
{{ block "body" . }}{{ end }}
{{- .FooterJS -}}
</body>
</html>`,
"header.a.js.tmpl": `alert("hello, {{ .Page.User }}");`,
"footer.a.js.tmpl": `document.write("this was called from the footer");`,
"header.b.js.tmpl": `console.log("header.b.js loaded");`,
"footer.b.js.tmpl": `document.write("footer.b.js loaded");`,
"header.c.js.tmpl": `console.log("header.c.js loaded");`,
"footer.c.js.tmpl": `document.write("footer.c.js loaded");`,
}
// 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 := InlineJSDisableImplicitDependencySite{
CachedSite: temple.NewCachedSite(templates),
Title: "My Example Site",
}
page := InlineJSDisableImplicitDependencyHomePage{
Layout: InlineJSDisableImplicitDependencyLayout{},
User: "Visitor",
}
temple.Render(ctx, os.Stdout, site, page)
//Output:
// <!doctype html>
// <html lang="en">
// <head>
// <title>My Example Site</title><script>
// alert("hello, Visitor");
// </script>
// <script>
// console.log("header.b.js loaded");
// </script>
// <script>
// console.log("header.c.js loaded");
// </script>
// </head>
// <body>
// Hello, Visitor. This is my home page.<script>
// document.write("this was called from the footer");
// </script>
// <script>
// document.write("footer.b.js loaded");
// </script>
// <script>
// document.write("footer.c.js loaded");
// </script>
// </body>
// </html>
}