phoenix/views/pages/templates.go
Ivan R. 6d25c4e8af
feat!: migrate to net/http
With the release of Go 1.22, the standard library now has
all the necessary functions that allow us to abandon Gin.

I hope this rewrite will lower the entry barrier for new developers.
As a nice bonus, the size of the program has decreased from 20 to 15.4 MB.

To solve issue #81, request logging has been improved.
Now all errors are displayed in the logs.
2024-03-25 15:52:18 +05:00

71 lines
1.3 KiB
Go

package pages
import (
"html/template"
"io"
"os"
"path"
log "github.com/sirupsen/logrus"
"github.com/ordinary-dev/phoenix/config"
)
var (
// Preloaded templates.
// The key is the file name.
templates = make(map[string]*template.Template)
)
// Preload all templates into `Templates` map.
func LoadTemplates() error {
// Fragments are reusable parts of templates.
fragments, err := os.ReadDir("templates/fragments")
if err != nil {
return err
}
var fragmentPaths []string
for _, f := range fragments {
fragmentPaths = append(
fragmentPaths,
path.Join("templates/fragments", f.Name()),
)
}
// Load pages.
files, err := os.ReadDir("templates")
if err != nil {
return err
}
for _, f := range files {
if f.IsDir() {
continue
}
templatePaths := []string{path.Join("templates", f.Name())}
templatePaths = append(templatePaths, fragmentPaths...)
tmpl, err := template.ParseFiles(templatePaths...)
if err != nil {
return err
}
templates[f.Name()] = tmpl
log.Infof("Template %v was loaded", f.Name())
}
return nil
}
func Render(template string, wr io.Writer, data map[string]any) error {
data["fontFamily"] = config.Cfg.FontFamily
if _, ok := data["title"]; !ok {
data["title"] = config.Cfg.Title
}
return templates[template].Execute(wr, data)
}