Init
This commit is contained in:
43
lib/templates.go
Normal file
43
lib/templates.go
Normal file
@ -0,0 +1,43 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
templatefs "atri.dad/pages/templates"
|
||||
)
|
||||
|
||||
func RenderTemplate(w http.ResponseWriter, layout string, partials []string, props interface{}) error {
|
||||
// Get the name of the current file
|
||||
_, filename, _, _ := runtime.Caller(1)
|
||||
page := filepath.Base(filename)
|
||||
page = page[:len(page)-len(filepath.Ext(page))] // remove the file extension
|
||||
|
||||
// Build the list of templates
|
||||
templates := []string{
|
||||
"layouts/" + layout + ".html",
|
||||
page + ".html",
|
||||
}
|
||||
for _, partial := range partials {
|
||||
templates = append(templates, "partials/"+partial+".html")
|
||||
}
|
||||
|
||||
// Parse the templates
|
||||
ts, err := template.ParseFS(templatefs.FS, templates...)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// Execute the layout template
|
||||
err = ts.ExecuteTemplate(w, layout, props)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user