2024-08-03 19:57:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type templateData struct {
|
|
|
|
StringMap map[string]string
|
|
|
|
IntMap map[string]int
|
|
|
|
FloatMap map[string]int
|
|
|
|
Data map[string]interface{}
|
|
|
|
CSRFToken string
|
|
|
|
Flash string
|
|
|
|
Warning string
|
|
|
|
Error string
|
|
|
|
API string
|
|
|
|
CSSVersion string
|
|
|
|
IsAuthenticated int
|
2024-08-06 19:18:32 +00:00
|
|
|
StripeSecretKey string
|
|
|
|
StripePubKey string
|
2024-08-03 19:57:24 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 18:52:46 +00:00
|
|
|
var functions = template.FuncMap{
|
|
|
|
"formatCurrency": formatCurrency,
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatCurrency(n int) string {
|
2024-08-11 10:30:57 +00:00
|
|
|
f := float32(n) / float32(100)
|
2024-08-06 18:52:46 +00:00
|
|
|
return fmt.Sprintf("€%.2f", f)
|
|
|
|
}
|
2024-08-03 19:57:24 +00:00
|
|
|
|
|
|
|
//go:embed templates
|
|
|
|
var templateFS embed.FS
|
|
|
|
|
|
|
|
func (app *application) addDefaultData(td *templateData, r *http.Request) *templateData {
|
2024-08-04 16:35:34 +00:00
|
|
|
td.API = app.config.api
|
2024-08-06 19:18:32 +00:00
|
|
|
td.StripePubKey = app.config.stripe.key
|
|
|
|
td.StripeSecretKey = app.config.stripe.secret
|
2024-08-21 19:39:59 +00:00
|
|
|
|
|
|
|
if app.Session.Exists(r.Context(), "userID") {
|
|
|
|
td.IsAuthenticated = 1
|
|
|
|
} else {
|
|
|
|
td.IsAuthenticated = 0
|
|
|
|
}
|
2024-08-04 16:17:56 +00:00
|
|
|
return td
|
2024-08-03 19:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *application) renderTemplate(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
page string,
|
|
|
|
td *templateData,
|
|
|
|
partials ...string,
|
|
|
|
) error {
|
|
|
|
var t *template.Template
|
|
|
|
var err error
|
2024-08-03 20:26:40 +00:00
|
|
|
templateToRender := fmt.Sprintf("templates/%s.page.gohtml", page)
|
2024-08-03 19:57:24 +00:00
|
|
|
|
|
|
|
_, templateInMap := app.templateCache[templateToRender]
|
|
|
|
|
|
|
|
if app.config.env == "production" && templateInMap {
|
|
|
|
t = app.templateCache[templateToRender]
|
|
|
|
} else {
|
|
|
|
t, err = app.parseTemplate(partials, page, templateToRender)
|
|
|
|
if err != nil {
|
|
|
|
app.errorLog.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if td == nil {
|
|
|
|
td = &templateData{}
|
|
|
|
}
|
|
|
|
|
|
|
|
td = app.addDefaultData(td, r)
|
|
|
|
|
|
|
|
err = t.Execute(w, td)
|
|
|
|
if err != nil {
|
|
|
|
app.errorLog.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *application) parseTemplate(
|
|
|
|
partials []string,
|
|
|
|
page, templateToRender string,
|
|
|
|
) (*template.Template, error) {
|
|
|
|
var t *template.Template
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// build partials
|
|
|
|
if len(partials) > 0 {
|
|
|
|
for i, x := range partials {
|
2024-08-06 11:56:39 +00:00
|
|
|
partials[i] = fmt.Sprintf("templates/%s.partial.gohtml", x)
|
2024-08-03 19:57:24 +00:00
|
|
|
}
|
2024-08-03 20:26:40 +00:00
|
|
|
t, err = template.New(fmt.Sprintf("%s.page.gohtml", page)).
|
2024-08-03 19:57:24 +00:00
|
|
|
Funcs(functions).
|
2024-08-03 20:26:40 +00:00
|
|
|
ParseFS(templateFS, "templates/base.layout.gohtml", strings.Join(partials, ","), templateToRender)
|
2024-08-03 19:57:24 +00:00
|
|
|
} else {
|
2024-08-03 20:26:40 +00:00
|
|
|
t, err = template.New(fmt.Sprintf("%s.page.gohtml", page)).
|
2024-08-03 19:57:24 +00:00
|
|
|
Funcs(functions).
|
2024-08-03 20:26:40 +00:00
|
|
|
ParseFS(templateFS, "templates/base.layout.gohtml", templateToRender)
|
2024-08-03 19:57:24 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
app.errorLog.Println(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
app.templateCache[templateToRender] = t
|
|
|
|
return t, nil
|
|
|
|
}
|