Immanuel Onyeka 3 роки тому
джерело
коміт
220ed768a6
2 змінених файлів з 87 додано та 1 видалено
  1. +1
    -1
      README.md
  2. +86
    -0
      view.go

+ 1
- 1
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.


+ 86
- 0
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)
}
}

Завантаження…
Відмінити
Зберегти