-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathtemplate.go
51 lines (44 loc) · 1.54 KB
/
template.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
// Copyright 2020 The WPT Dashboard Project. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package webapp
import (
"embed"
"html/template"
"net/http"
"github.com/web-platform-tests/wpt.fyi/shared"
)
// templates contains all of the template objects parsed from templates/*,
// created once at startup. Use RenderTemplate to render responses.
var templates *template.Template
//go:embed templates/*.html
var htmlTemplates embed.FS
func init() {
templates = template.New("all.html")
_, err := templates.ParseFS(htmlTemplates, "templates/*.html")
if err != nil {
panic(err)
}
}
type templateData struct {
Data interface{}
User *shared.User
}
// RenderTemplate renders an HTML template to a response. The provided data
// will be available in the Data field in the template. There are some
// additional fields extracted from the request (e.g. User) available in the
// template if the request is not nil.
// If an error is encountered, appropriate error codes and messages will be set
// on the response; do not write additional data to the response after calling
// this function.
func RenderTemplate(w http.ResponseWriter, r *http.Request, name string, data interface{}) {
tdata := templateData{Data: data}
if r != nil {
ctx := r.Context()
ds := shared.NewAppEngineDatastore(ctx, false)
tdata.User, _ = shared.GetUserFromCookie(ctx, ds, r)
}
if err := templates.ExecuteTemplate(w, name, tdata); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}