From 220ed768a66a1853a95ccac2ec7aa08201c2713e Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Sun, 18 Jul 2021 15:22:09 -0400 Subject: [PATCH] Add go code --- README.md | 2 +- view.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 view.go diff --git a/README.md b/README.md index 226a2c0..c2a92ae 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Code Examples Some sample code taken from sites I've completed. They're written in Javascript, PHP, and SCSS. The examples are incomplete and meant for -demonstrative purposes only as I'm not comfortable share complete code of a +demonstrative purposes only as I'm not comfortable sharing complete code of a live, potentialy profitable project. diff --git a/view.go b/view.go new file mode 100644 index 0000000..be5d67c --- /dev/null +++ b/view.go @@ -0,0 +1,86 @@ +package view + +import ( + "html/template" + "net/http" + "log" +) + +var ( + available = true + matrix = "zgvhdgh0b3nwawv6" +) + +type Page struct { + tpl *template.Template + Title string + Name string + Matrix string + Available bool +} + +var titles = map[string]string { + "home": "Home", + "services": "Services", + "contact": "Contact", + "web": "Web Development", + "projects": "Projects", + "about": "About", +} + +var paths = map[string]string { + "home": "view/home.tpl", + "services": "view/services.tpl", + "contact": "view/contact.tpl", + "web": "view/web.tpl", + "projects": "view/projects.tpl", + "about": "view/about.tpl", +} + +var pages = map[string]Page { + "home": cache("home"), + "services": cache("services"), + "contact": cache("contact"), + "web": cache("web"), + "projects": cache("projects"), + "about": cache("about"), +} + +func parse(path string) *template.Template { + var paths = []string{"view/master.tpl", path} + tpl, err := template.ParseFiles(paths...) + if err != nil { + log.Print(err, "| Could not parse:", path) + } + return tpl +} + +func Create(name string) Page { + tpl := parse(paths[name]) + title := titles[name] + return Page{tpl: tpl, + Title: title, + Name: name, + Matrix: matrix, + Available: available} +} + +func cache(name string) Page { + var p = []string{"view/master.tpl", paths[name]} + tpl := template.Must(template.ParseFiles(p...)) + return Page{tpl: tpl, + Title: titles[name], + Name: name, + Matrix: matrix, + Available: available} +} + +func Get(name string) Page { + return pages[name] +} +func (page Page) Render(w http.ResponseWriter) { + err := page.tpl.Execute(w, page) + if err != nil { + log.Print(err) + } +}