summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorMitja Felicijan <m@mitjafelicijan.com>2023-08-08 15:48:05 +0200
committerMitja Felicijan <m@mitjafelicijan.com>2023-08-08 15:48:05 +0200
commita506043a205fa6a33941d641b76d02a08c470d08 (patch)
tree339caf235ec7c9e4718f24b903adce4fa33afb43 /main.go
parent3302e2b42cf695f1c83d4d0f0604fa64a05cec4e (diff)
downloadjbmafp-a506043a205fa6a33941d641b76d02a08c470d08.tar.gz
Added includes option
All files in `templates/includes` can be included from a template now. This is done with `{{ template "file.html" }}`.
Diffstat (limited to 'main.go')
-rw-r--r--main.go41
1 files 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)
}