~emersion/drmdb

drmdb/template.go -rw-r--r-- 729 bytes View raw
31ca651fSimon Ser drmtree: handle USB devices 21 days ago
                                                                                
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
package drmdb

import (
	"fmt"
	"html/template"
	"io"
	"strings"

	"github.com/labstack/echo/v4"
)

type tmpl struct {
	t *template.Template
}

var _ = fmt.Printf

func (t *tmpl) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
	return t.t.ExecuteTemplate(w, name, data)
}

func loadTemplates() (*tmpl, error) {
	t, err := template.New("drmdb").Funcs(template.FuncMap{
		"join": strings.Join,
		"pct": func(num, tot int) float32 {
			return 100 * float32(num) / float32(tot)
		},
		"pctColor": func(pct float32) template.CSS {
			f := float32(pct) / 100
			hue := int(f * 120)
			return template.CSS(fmt.Sprintf("hsl(%d, 100%%, 30%%)", hue))
		},
	}).ParseGlob("public/*.html")

	return &tmpl{t}, err
}