Compare commits
2 commits
e6b5ea3921
...
2ba0a0024e
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ba0a0024e | |||
| e7e420cd47 |
6 changed files with 37 additions and 16 deletions
|
|
@ -1,3 +1,4 @@
|
|||
extension = "md"
|
||||
notesdir = "saved_notes"
|
||||
templatesdir = "templates"
|
||||
basetemplate = "base.tmpl.html"
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ type Config struct {
|
|||
Extension string
|
||||
NotesDir string
|
||||
TemplatesDir string
|
||||
BaseTemplate string
|
||||
}
|
||||
|
||||
var Conf Config
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package views
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
|
@ -11,6 +10,7 @@ import (
|
|||
urls "gitea.gwairfelin.com/max/gispatcho"
|
||||
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
||||
"gitea.gwairfelin.com/max/gonotes/internal/notes"
|
||||
"gitea.gwairfelin.com/max/gonotes/internal/templ"
|
||||
)
|
||||
|
||||
var myurls urls.URLs
|
||||
|
|
@ -37,9 +37,9 @@ func view(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
context := map[string]any{"note": note, "urlEdit": urlEdit}
|
||||
context := templ.Ctx{"note": note, "urlEdit": urlEdit}
|
||||
note.Render()
|
||||
err = renderTemplate(w, "view.html", context)
|
||||
err = templ.RenderTemplate(w, "view.tmpl.html", context)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
||||
|
|
@ -55,8 +55,8 @@ func edit(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
urlSave := myurls.Reverse("save", urls.Repl{"note": title})
|
||||
context := map[string]any{"note": note, "urlSave": urlSave}
|
||||
err = renderTemplate(w, "edit.html", context)
|
||||
context := templ.Ctx{"note": note, "urlSave": urlSave}
|
||||
err = templ.RenderTemplate(w, "edit.tmpl.html", context)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
|
||||
|
|
@ -89,20 +89,10 @@ func list(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
err = renderTemplate(w, "list.tmpl.html", map[string]any{"titles": titles})
|
||||
err = templ.RenderTemplate(w, "list.tmpl.html", templ.Ctx{"titles": titles})
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func renderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
||||
files := []string{
|
||||
filepath.Join(conf.Conf.TemplatesDir, "base.tmpl.html"),
|
||||
filepath.Join(conf.Conf.TemplatesDir, tmpl),
|
||||
}
|
||||
t, err := template.ParseFiles(files...)
|
||||
t.ExecuteTemplate(w, "base", context)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
29
internal/templ/templ.go
Normal file
29
internal/templ/templ.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package templ
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gitea.gwairfelin.com/max/gonotes/internal/conf"
|
||||
)
|
||||
|
||||
type Ctx map[string]any
|
||||
|
||||
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
||||
files := []string{
|
||||
filepath.Join(conf.Conf.TemplatesDir, conf.Conf.BaseTemplate),
|
||||
filepath.Join(conf.Conf.TemplatesDir, tmpl),
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
_, err := os.Stat(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
t, err := template.ParseFiles(files...)
|
||||
t.ExecuteTemplate(w, "base", context)
|
||||
return err
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue