2024-03-25 15:52:18 +05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-01 23:55:43 +05:00
|
|
|
func Render(template string, wr io.Writer, data map[string]any) {
|
2024-03-25 15:52:18 +05:00
|
|
|
data["fontFamily"] = config.Cfg.FontFamily
|
|
|
|
|
|
|
|
if _, ok := data["title"]; !ok {
|
|
|
|
data["title"] = config.Cfg.Title
|
|
|
|
}
|
|
|
|
|
2024-04-01 23:55:43 +05:00
|
|
|
err := templates[template].Execute(wr, data)
|
|
|
|
if err != nil {
|
|
|
|
log.WithField("template", template).Error(err)
|
|
|
|
}
|
2024-03-25 15:52:18 +05:00
|
|
|
}
|