aboutsummaryrefslogtreecommitdiff
path: root/cmd/http/render.go
blob: 9c01e57cd742c7601db6ce8c449f7b7cf2ebc5e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main

import (
	"net/http"
	"strings"
	"text/template"
)

func renderTemplate(w http.ResponseWriter, tmplPath string, p *Posts) (err error) {
	t, err := template.ParseFiles("html/master.tmpl.html", "html/"+tmplPath+".tmpl.html")
	if err != nil {
		return err
	}

	splitPath := strings.Split(tmplPath, "/")

	data := make(map[string]interface{})

	// If were loading the index, set page to 'Index' and pass through all posts.
	// Otherwise, set page to 'Projects' and pass through the first post (should only be one
	// coming in)
	if splitPath[0] == "index" {
		data["Page"] = "Index"
		data["Posts"] = p
	} else if splitPath[0] == "about" {
		data["Page"] = "About"
		data["Posts"] = nil
	} else {
		data["Page"] = "Project"
		data["Post"] = p.Contents[0]
	}

	err = t.Execute(w, data)
	if err != nil {
		return err
	}

	return nil
}