aboutsummaryrefslogtreecommitdiff
path: root/internal/renderer/renderer.go
blob: 92af85c1ca74a2109939c4fe2ced38fd61872052 (plain)
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
package renderer

import (
	"errors"
	"projecty/internal/authentication"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/template/html/v2"
)

func ViewEngineStart() *html.Engine {

	viewEngine := html.New("./cmd/web/templates", ".tmpl")

	viewEngine.AddFunc("IsAuthenticated", func(c *fiber.Ctx) bool {
		isAuthenticated, _ := authentication.AuthGet(c)
		return isAuthenticated
	})

	viewEngine.AddFunc("Iterate", func(start int, end int) []int {
		n := end - start + 1
		result := make([]int, n)
		for i := 0; i < n; i++ {
			result[i] = start + i
		}
		return result
	})

	viewEngine.AddFunc("Dict", func(values ...interface{}) (map[string]interface{}, error) {
		if len(values)%2 != 0 {
			return nil, errors.New("invalid dict call")
		}
		dict := make(map[string]interface{}, len(values)/2)
		for i := 0; i < len(values); i += 2 {
			key, ok := values[i].(string)
			if !ok {
				return nil, errors.New("dict keys must be strings")
			}
			dict[key] = values[i+1]
		}
		return dict, nil
	})

	return viewEngine
}