about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-05-15 12:35:40 +0200
committerAlan Pearce2024-05-15 12:35:40 +0200
commit149cbe7681607eb48f47df14c7a39e6f289fb7b6 (patch)
tree97c00c61a61f38001205b39bdf15cf7bdb16784c
parent5624cf8f6427064dd17de40acd1d3fdfe8332e96 (diff)
downloadsearchix-149cbe7681607eb48f47df14c7a39e6f289fb7b6.tar.lz
searchix-149cbe7681607eb48f47df14c7a39e6f289fb7b6.tar.zst
searchix-149cbe7681607eb48f47df14c7a39e6f289fb7b6.zip
refactor(templates): render partials with less hackiness
-rw-r--r--frontend/templates/blocks/options.gotmpl1
-rw-r--r--frontend/templates/blocks/search.gotmpl3
-rw-r--r--internal/server/mux.go6
-rw-r--r--internal/server/templates.go13
4 files changed, 11 insertions, 12 deletions
diff --git a/frontend/templates/blocks/options.gotmpl b/frontend/templates/blocks/options.gotmpl
index ff8805c..ad92c17 100644
--- a/frontend/templates/blocks/options.gotmpl
+++ b/frontend/templates/blocks/options.gotmpl
@@ -1,4 +1,3 @@
-{{- template "results" . -}}
 {{- define "results" }}
   {{- if gt .Results.Total 0 }}
     <section id="results">
diff --git a/frontend/templates/blocks/search.gotmpl b/frontend/templates/blocks/search.gotmpl
index feb77cc..3ad05bf 100644
--- a/frontend/templates/blocks/search.gotmpl
+++ b/frontend/templates/blocks/search.gotmpl
@@ -1,6 +1,3 @@
-{{- template "main" . }}
-{{- template "js" . }}
-
 {{- define "main" }}
   <form id="search">
     <label for="query">{{ .Source.Name }} option search</label>
diff --git a/internal/server/mux.go b/internal/server/mux.go
index f37bd6e..aa2c1a6 100644
--- a/internal/server/mux.go
+++ b/internal/server/mux.go
@@ -106,7 +106,7 @@ func NewMux(
 			Sources:       config.Importer.Sources,
 			Version:       *versionInfo,
 		}
-		err := templates["index"].ExecuteTemplate(w, "index.gotmpl", indexData)
+		err := templates["index"].Execute(w, indexData)
 		if err != nil {
 			http.Error(w, err.Error(), http.StatusInternalServerError)
 		}
@@ -192,9 +192,9 @@ func NewMux(
 
 			if r.Header.Get("Fetch") == "true" {
 				w.Header().Add("Content-Type", "text/html; charset=utf-8")
-				err = templates["options"].ExecuteTemplate(w, "options.gotmpl", tdata)
+				err = templates["options"].ExecuteTemplate(w, "results", tdata)
 			} else {
-				err = templates["options"].ExecuteTemplate(w, "index.gotmpl", tdata)
+				err = templates["options"].Execute(w, tdata)
 			}
 			if err != nil {
 				slog.Error("template error", "template", "options", "error", err)
diff --git a/internal/server/templates.go b/internal/server/templates.go
index bd3ad90..d8ffcf5 100644
--- a/internal/server/templates.go
+++ b/internal/server/templates.go
@@ -47,7 +47,10 @@ var templateFuncs = template.FuncMap{
 	},
 }
 
-func loadTemplate(layoutFile string, filename string) (*template.Template, error) {
+func loadTemplate(
+	layoutFile string,
+	filenames ...string,
+) (*template.Template, error) {
 	tpl := template.New("index.gotmpl")
 
 	tpl.Funcs(templateFuncs)
@@ -56,8 +59,8 @@ func loadTemplate(layoutFile string, filename string) (*template.Template, error
 		return nil, errors.WithMessage(err, "could not parse layout template")
 	}
 
-	if filename != "" {
-		_, err = tpl.ParseFS(frontend.Files, filename)
+	if len(filenames) > 0 {
+		_, err = tpl.ParseFS(frontend.Files, filenames...)
 		if err != nil {
 			return nil, errors.WithMessage(err, "could not parse template")
 		}
@@ -72,7 +75,7 @@ func loadTemplates() (TemplateCollection, error) {
 
 	layoutFile := path.Join(templateDir, "index.gotmpl")
 
-	index, err := loadTemplate(layoutFile, "")
+	index, err := loadTemplate(layoutFile)
 	if err != nil {
 		return nil, err
 	}
@@ -84,7 +87,7 @@ func loadTemplates() (TemplateCollection, error) {
 		return nil, errors.WithMessage(err, "could not glob block templates")
 	}
 	for _, fullname := range templatePaths {
-		tpl, err := loadTemplate(layoutFile, glob)
+		tpl, err := loadTemplate(layoutFile, glob, fullname)
 		if err != nil {
 			return nil, err
 		}