diff options
-rw-r--r-- | frontend/templates/blocks/options.gotmpl | 1 | ||||
-rw-r--r-- | internal/server/server.go | 6 | ||||
-rw-r--r-- | internal/server/templates.go | 27 |
3 files changed, 15 insertions, 19 deletions
diff --git a/frontend/templates/blocks/options.gotmpl b/frontend/templates/blocks/options.gotmpl index a4c4f12..f89a83d 100644 --- a/frontend/templates/blocks/options.gotmpl +++ b/frontend/templates/blocks/options.gotmpl @@ -1,3 +1,4 @@ +{{ template "results" . }} {{ define "results" }} {{- range .Results }} <details id="{{ .Option }}"> diff --git a/internal/server/server.go b/internal/server/server.go index b6270ef..a6f22e2 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -107,7 +107,7 @@ func New(runtimeConfig *Config) (*Server, error) { LiveReload: jsSnippet, } mux.HandleFunc("/{$}", func(w http.ResponseWriter, _ *http.Request) { - err := templates["index"].Execute(w, indexData) + err := templates["index"].ExecuteTemplate(w, "index.gotmpl", indexData) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -132,9 +132,9 @@ func New(runtimeConfig *Config) (*Server, error) { var err error if r.Header.Get("Fetch") == "true" { w.Header().Add("Content-Type", "text/html; charset=utf-8") - err = templates["options"].ExecuteTemplate(w, "results", tdata) + err = templates["options"].Execute(w, tdata) } else { - err = templates["options"].ExecuteTemplate(w, "index", tdata) + err = templates["options"].ExecuteTemplate(w, "index.gotmpl", tdata) } if err != nil { slog.Error(fmt.Sprintf("template error: %v", err)) diff --git a/internal/server/templates.go b/internal/server/templates.go index 0b39729..71fc5c7 100644 --- a/internal/server/templates.go +++ b/internal/server/templates.go @@ -5,7 +5,6 @@ import ( "html" "html/template" "log/slog" - "os" "path" "path/filepath" "searchix/internal/options" @@ -35,15 +34,12 @@ var templateFuncs = template.FuncMap{ }, } -func loadTemplate(filename string) (*template.Template, error) { - text, err := os.ReadFile(filename) - if err != nil { - return nil, errors.WithMessage(err, "could not read template") - } - name, _ := strings.CutSuffix(path.Base(filename), ".gotmpl") - tpl := template.New(name) +func loadTemplate(filenames ...string) (*template.Template, error) { + tpl := template.New(path.Base(filenames[0])) + tpl.Funcs(templateFuncs) - _, err = tpl.Parse(string(text)) + + _, err := tpl.ParseFiles(filenames...) if err != nil { return nil, errors.WithMessage(err, "could not parse template") } @@ -55,7 +51,9 @@ func loadTemplates() (TemplateCollection, error) { templateDir := path.Join("frontend", "templates") templates := make(TemplateCollection, 0) - index, err := loadTemplate(path.Join(templateDir, "index.gotmpl")) + layoutFile := path.Join(templateDir, "index.gotmpl") + + index, err := loadTemplate(layoutFile) if err != nil { return nil, err } @@ -66,15 +64,12 @@ func loadTemplates() (TemplateCollection, error) { return nil, errors.WithMessage(err, "could not glob block templates") } for _, fullname := range templatePaths { - tpl, err := loadTemplate(fullname) + tpl, err := loadTemplate(fullname, layoutFile) if err != nil { return nil, err } - _, err = tpl.AddParseTree("index", index.Tree) - if err != nil { - return nil, errors.WithMessage(err, "could not add index template") - } - templates[tpl.Name()] = tpl + name, _ := strings.CutSuffix(path.Base(fullname), ".gotmpl") + templates[name] = tpl } return templates, nil |