Compare commits

..

No commits in common. "2ba0a0024ecb333fac47adb49e1c8c5856925b92" and "e6b5ea3921f3cb75ad5bfc72befdb0d65820b6c1" have entirely different histories.

6 changed files with 16 additions and 37 deletions

View file

@ -1,4 +1,3 @@
extension = "md" extension = "md"
notesdir = "saved_notes" notesdir = "saved_notes"
templatesdir = "templates" templatesdir = "templates"
basetemplate = "base.tmpl.html"

View file

@ -11,7 +11,6 @@ type Config struct {
Extension string Extension string
NotesDir string NotesDir string
TemplatesDir string TemplatesDir string
BaseTemplate string
} }
var Conf Config var Conf Config

View file

@ -1,6 +1,7 @@
package views package views
import ( import (
"html/template"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -10,7 +11,6 @@ import (
urls "gitea.gwairfelin.com/max/gispatcho" urls "gitea.gwairfelin.com/max/gispatcho"
"gitea.gwairfelin.com/max/gonotes/internal/conf" "gitea.gwairfelin.com/max/gonotes/internal/conf"
"gitea.gwairfelin.com/max/gonotes/internal/notes" "gitea.gwairfelin.com/max/gonotes/internal/notes"
"gitea.gwairfelin.com/max/gonotes/internal/templ"
) )
var myurls urls.URLs var myurls urls.URLs
@ -37,9 +37,9 @@ func view(w http.ResponseWriter, r *http.Request) {
return return
} }
context := templ.Ctx{"note": note, "urlEdit": urlEdit} context := map[string]any{"note": note, "urlEdit": urlEdit}
note.Render() note.Render()
err = templ.RenderTemplate(w, "view.tmpl.html", context) err = renderTemplate(w, "view.html", context)
if err != nil { if err != nil {
log.Print(err.Error()) log.Print(err.Error())
http.Error(w, "Couldn't load template", http.StatusInternalServerError) 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}) urlSave := myurls.Reverse("save", urls.Repl{"note": title})
context := templ.Ctx{"note": note, "urlSave": urlSave} context := map[string]any{"note": note, "urlSave": urlSave}
err = templ.RenderTemplate(w, "edit.tmpl.html", context) err = renderTemplate(w, "edit.html", context)
if err != nil { if err != nil {
log.Print(err.Error()) log.Print(err.Error())
http.Error(w, "Couldn't load template", http.StatusInternalServerError) http.Error(w, "Couldn't load template", http.StatusInternalServerError)
@ -89,10 +89,20 @@ func list(w http.ResponseWriter, r *http.Request) {
} }
} }
err = templ.RenderTemplate(w, "list.tmpl.html", templ.Ctx{"titles": titles}) err = renderTemplate(w, "list.tmpl.html", map[string]any{"titles": titles})
if err != nil { if err != nil {
log.Print(err.Error()) log.Print(err.Error())
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return 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
}

View file

@ -1,29 +0,0 @@
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
}