2024-06-26 20:06:32 +00:00
|
|
|
package render
|
2024-06-26 19:56:58 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-27 11:19:38 +00:00
|
|
|
"bytes"
|
2024-07-02 11:57:45 +00:00
|
|
|
"errors"
|
2024-07-01 20:34:16 +00:00
|
|
|
"fmt"
|
2024-06-30 14:41:46 +00:00
|
|
|
"go-udemy-web-1/internal/config"
|
2024-07-03 08:03:25 +00:00
|
|
|
"go-udemy-web-1/internal/helpers"
|
2024-06-30 14:41:46 +00:00
|
|
|
"go-udemy-web-1/internal/models"
|
2024-06-26 19:56:58 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2024-06-27 11:19:38 +00:00
|
|
|
"path/filepath"
|
2024-06-30 08:31:15 +00:00
|
|
|
|
|
|
|
"github.com/justinas/nosurf"
|
2024-06-26 19:56:58 +00:00
|
|
|
)
|
|
|
|
|
2024-07-01 20:34:16 +00:00
|
|
|
var functions = template.FuncMap{}
|
|
|
|
|
|
|
|
var (
|
|
|
|
app *config.AppConfig
|
|
|
|
pathToTemplates = "./templates"
|
|
|
|
)
|
2024-06-27 12:03:43 +00:00
|
|
|
|
2024-07-07 20:37:48 +00:00
|
|
|
// NewRenderer sets the config for the template package
|
|
|
|
func NewRenderer(a *config.AppConfig) {
|
2024-06-27 12:03:43 +00:00
|
|
|
app = a
|
|
|
|
}
|
|
|
|
|
2024-06-28 08:48:52 +00:00
|
|
|
// AddDefaultData adds default template data
|
2024-06-30 08:31:15 +00:00
|
|
|
func AddDefaultData(td *models.TemplateData, r *http.Request) *models.TemplateData {
|
2024-07-01 12:28:00 +00:00
|
|
|
td.Flash = app.Session.PopString(r.Context(), "flash")
|
|
|
|
td.Warning = app.Session.PopString(r.Context(), "warning")
|
|
|
|
td.Error = app.Session.PopString(r.Context(), "error")
|
2024-06-30 08:31:15 +00:00
|
|
|
td.CSRFToken = nosurf.Token(r)
|
2024-06-28 08:48:52 +00:00
|
|
|
return td
|
|
|
|
}
|
|
|
|
|
2024-07-07 20:37:48 +00:00
|
|
|
// Template renders a HTML template file
|
|
|
|
func Template(w http.ResponseWriter, r *http.Request, tmpl string, td *models.TemplateData) error {
|
2024-06-27 12:03:43 +00:00
|
|
|
var tc map[string]*template.Template
|
|
|
|
if app.UseCache {
|
|
|
|
// get the template cache from the app config
|
|
|
|
tc = app.TemplateCahce
|
|
|
|
} else {
|
|
|
|
tc, _ = CreateTemplateCache()
|
2024-06-27 11:19:38 +00:00
|
|
|
}
|
2024-06-27 12:03:43 +00:00
|
|
|
|
2024-06-27 11:19:38 +00:00
|
|
|
// get requested template from cache
|
|
|
|
t, ok := tc[tmpl]
|
|
|
|
if !ok {
|
2024-07-02 11:57:45 +00:00
|
|
|
return errors.New("could not get template from template cache")
|
2024-06-26 20:50:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 11:19:38 +00:00
|
|
|
// Write to a buffer to make sure that the template can be read and
|
|
|
|
// written successfully
|
|
|
|
buf := new(bytes.Buffer)
|
2024-06-28 08:48:52 +00:00
|
|
|
|
2024-06-30 08:31:15 +00:00
|
|
|
td = AddDefaultData(td, r)
|
2024-06-28 08:48:52 +00:00
|
|
|
|
|
|
|
err := t.Execute(buf, td)
|
2024-06-27 11:19:38 +00:00
|
|
|
if err != nil {
|
2024-07-03 08:03:25 +00:00
|
|
|
helpers.ServerError(w, err)
|
2024-07-02 11:57:45 +00:00
|
|
|
return err
|
2024-06-27 11:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// render the template
|
|
|
|
_, err = buf.WriteTo(w)
|
2024-06-26 19:56:58 +00:00
|
|
|
if err != nil {
|
2024-07-03 08:03:25 +00:00
|
|
|
helpers.ServerError(w, err)
|
2024-07-02 11:57:45 +00:00
|
|
|
return err
|
2024-06-26 20:50:04 +00:00
|
|
|
}
|
2024-07-02 11:57:45 +00:00
|
|
|
|
|
|
|
return nil
|
2024-06-26 20:50:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 12:03:43 +00:00
|
|
|
func CreateTemplateCache() (map[string]*template.Template, error) {
|
2024-06-27 11:19:38 +00:00
|
|
|
myCache := map[string]*template.Template{}
|
|
|
|
|
2024-07-01 20:34:16 +00:00
|
|
|
// get all of the files named *.page.tmpl from templates
|
|
|
|
pages, err := filepath.Glob(fmt.Sprintf("%s/*.page.tmpl", pathToTemplates))
|
2024-06-27 11:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return myCache, err
|
2024-06-26 19:56:58 +00:00
|
|
|
}
|
2024-06-26 20:50:04 +00:00
|
|
|
|
2024-06-27 11:19:38 +00:00
|
|
|
// range through all files ending with *page.tmpl
|
|
|
|
for _, page := range pages {
|
|
|
|
name := filepath.Base(page)
|
2024-07-01 20:34:16 +00:00
|
|
|
ts, err := template.New(name).Funcs(functions).ParseFiles(page)
|
2024-06-27 11:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return myCache, err
|
|
|
|
}
|
|
|
|
|
2024-07-01 20:34:16 +00:00
|
|
|
matches, err := filepath.Glob(fmt.Sprintf("%s/*.layout.tmpl", pathToTemplates))
|
2024-06-27 11:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return myCache, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 0 {
|
2024-07-01 20:34:16 +00:00
|
|
|
ts, err = ts.ParseGlob(fmt.Sprintf("%s/*.layout.tmpl", pathToTemplates))
|
2024-06-27 11:19:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return myCache, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
myCache[name] = ts
|
2024-06-26 20:50:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 11:19:38 +00:00
|
|
|
return myCache, nil
|
2024-06-26 19:56:58 +00:00
|
|
|
}
|