From a506043a205fa6a33941d641b76d02a08c470d08 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Tue, 8 Aug 2023 15:48:05 +0200 Subject: Added includes option All files in `templates/includes` can be included from a template now. This is done with `{{ template "file.html" }}`. --- main.go | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 8227323..4b9a4df 100644 --- a/main.go +++ b/main.go @@ -94,6 +94,27 @@ func cleanHTMLTags(htmlString string) string { return cleanString } +func includeTemplateList(projectRoot string) []string { + var templateFiles []string + includesTemplatePathname := path.Join(projectRoot, "templates/includes") + err := filepath.Walk(includesTemplatePathname, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if filepath.Ext(path) == ".html" { + templateFiles = append(templateFiles, path) + } + + return nil + }) + + if err != nil { + panic(err) + } + + return templateFiles +} + func simpleServer(projectRoot string) { fs := http.FileServer(http.Dir(path.Join(projectRoot, "public"))) http.Handle("/", fs) @@ -109,6 +130,11 @@ func initializeProject(projectRoot string) { return } + if err := os.Mkdir(path.Join(projectRoot, "templates", "includes"), 0755); err != nil && !os.IsExist(err) { + log.Println("Error creating directory:", err) + return + } + if err := os.Mkdir(path.Join(projectRoot, "content"), 0755); err != nil && !os.IsExist(err) { log.Println("Error creating directory:", err) return @@ -253,7 +279,12 @@ func buildProject(projectRoot string) { pageTemplateFilename := fmt.Sprintf("%s.html", page.Meta["type"].(string)) templatePathname := path.Join(projectRoot, "templates", pageTemplateFilename) baseTemplatePathname := path.Join(projectRoot, "templates/base.html") - t, err := template.ParseFiles(baseTemplatePathname, templatePathname) + + templates := includeTemplateList(projectRoot) + templates = append([]string{templatePathname}, templates...) + templates = append([]string{baseTemplatePathname}, templates...) + + t, err := template.ParseFiles(templates...) if err != nil { panic(err) } @@ -293,10 +324,16 @@ func buildProject(projectRoot string) { // Generates index page. { + log.Println("Writing index...") templatePathname := path.Join(projectRoot, "templates/index.html") baseTemplatePathname := path.Join(projectRoot, "templates/base.html") - t, err := template.ParseFiles(baseTemplatePathname, templatePathname) + + templates := includeTemplateList(projectRoot) + templates = append([]string{templatePathname}, templates...) + templates = append([]string{baseTemplatePathname}, templates...) + + t, err := template.ParseFiles(templates...) if err != nil { panic(err) } -- cgit v1.2.3