Added includes option

All files in `templates/includes` can be included from a template now. This is done with `{{ template "file.html" }}`.

Author Mitja Felicijan <m@mitjafelicijan.com> 2023-08-08 15:48:05 +0200
Committer Mitja Felicijan <m@mitjafelicijan.com> 2023-08-08 15:48:05 +0200
Commit a506043a205fa6a33941d641b76d02a08c470d08 (patch)
-rw-r--r-- main.go 41
1 files changed, 39 insertions, 2 deletions
diff --git a/main.go b/main.go
...
94
	return cleanString
94
	return cleanString
95
}
95
}
96
  
96
  
  
97
func includeTemplateList(projectRoot string) []string {
  
98
	var templateFiles []string
  
99
	includesTemplatePathname := path.Join(projectRoot, "templates/includes")
  
100
	err := filepath.Walk(includesTemplatePathname, func(path string, info os.FileInfo, err error) error {
  
101
		if err != nil {
  
102
			return err
  
103
		}
  
104
		if filepath.Ext(path) == ".html" {
  
105
			templateFiles = append(templateFiles, path)
  
106
		}
  
107
  
  
108
		return nil
  
109
	})
  
110
  
  
111
	if err != nil {
  
112
		panic(err)
  
113
	}
  
114
  
  
115
	return templateFiles
  
116
}
  
117
  
97
func simpleServer(projectRoot string) {
118
func simpleServer(projectRoot string) {
98
	fs := http.FileServer(http.Dir(path.Join(projectRoot, "public")))
119
	fs := http.FileServer(http.Dir(path.Join(projectRoot, "public")))
99
	http.Handle("/", fs)
120
	http.Handle("/", fs)
...
105
	log.Println("Initializing new project")
126
	log.Println("Initializing new project")
106
  
127
  
107
	if err := os.Mkdir(path.Join(projectRoot, "templates"), 0755); err != nil && !os.IsExist(err) {
128
	if err := os.Mkdir(path.Join(projectRoot, "templates"), 0755); err != nil && !os.IsExist(err) {
  
129
		log.Println("Error creating directory:", err)
  
130
		return
  
131
	}
  
132
  
  
133
	if err := os.Mkdir(path.Join(projectRoot, "templates", "includes"), 0755); err != nil && !os.IsExist(err) {
108
		log.Println("Error creating directory:", err)
134
		log.Println("Error creating directory:", err)
109
		return
135
		return
110
	}
136
	}
...
253
			pageTemplateFilename := fmt.Sprintf("%s.html", page.Meta["type"].(string))
279
			pageTemplateFilename := fmt.Sprintf("%s.html", page.Meta["type"].(string))
254
			templatePathname := path.Join(projectRoot, "templates", pageTemplateFilename)
280
			templatePathname := path.Join(projectRoot, "templates", pageTemplateFilename)
255
			baseTemplatePathname := path.Join(projectRoot, "templates/base.html")
281
			baseTemplatePathname := path.Join(projectRoot, "templates/base.html")
256
			t, err := template.ParseFiles(baseTemplatePathname, templatePathname)
282
  
  
283
			templates := includeTemplateList(projectRoot)
  
284
			templates = append([]string{templatePathname}, templates...)
  
285
			templates = append([]string{baseTemplatePathname}, templates...)
  
286
  
  
287
			t, err := template.ParseFiles(templates...)
257
			if err != nil {
288
			if err != nil {
258
				panic(err)
289
				panic(err)
259
			}
290
			}
...
293
  
324
  
294
	// Generates index page.
325
	// Generates index page.
295
	{
326
	{
  
327
  
296
		log.Println("Writing index...")
328
		log.Println("Writing index...")
297
		templatePathname := path.Join(projectRoot, "templates/index.html")
329
		templatePathname := path.Join(projectRoot, "templates/index.html")
298
		baseTemplatePathname := path.Join(projectRoot, "templates/base.html")
330
		baseTemplatePathname := path.Join(projectRoot, "templates/base.html")
299
		t, err := template.ParseFiles(baseTemplatePathname, templatePathname)
331
  
  
332
		templates := includeTemplateList(projectRoot)
  
333
		templates = append([]string{templatePathname}, templates...)
  
334
		templates = append([]string{baseTemplatePathname}, templates...)
  
335
  
  
336
		t, err := template.ParseFiles(templates...)
300
		if err != nil {
337
		if err != nil {
301
			panic(err)
338
			panic(err)
302
		}
339
		}
...