Add base template & update, some new data & accessing categs

This commit is contained in:
mikgor 2018-10-25 19:23:49 +02:00
parent 198c34bc28
commit 72deabcada
4 changed files with 57 additions and 7 deletions

View File

@ -7,7 +7,7 @@ import (
func initializePages() {
Pages = make(map[string]Page)
Pages["index"] = Page{Path: "/", Template: "templates/index.html", Data: map[string]interface{}{"test": '1'}}
Pages["index"] = Page{Path: "/", Template: "templates/index.html", Data: map[string]interface{}{"categories": Categories}}
}
func initializeData() {
@ -22,11 +22,32 @@ func initializeData() {
{8, "Klawiatury"},
{9, "Akcesoria"},
}
Products = []Product {
{
1,
GetCategoryByName("Laptopy"),
"Apple MacBook Air 128 GB",
"Procesor Intel Core i5</br>Pamięć 8 GB</br>Dysk SSD M.2 PCIe 128 GB</br>Grafika Intel HD Graphics 6000</br>Typ ekranu Błyszczący, LED</br>System macOS Sierra",
"https://cdn.x-kom.pl/i/setup/images/prod/big/product-big,apple-macbook-air-i58gb128gbhd-6000mac-os-368639,pr_2016_4_28_12_39_54_140.jpg",
11,
3999,
},
{
2,
GetCategoryByName("Komputery"),
"Dell Inspiron 3670",
"System Microsoft Windows 10 Home PL</br>Procesor Intel Core i7-8700</br>Grafika NVIDIA GeForce GTX 1050Ti, + Intel UHD Graphics 630</br>Pamięć RAM 8 GB</br>Dysk SSD PCIe 128 GB</br>Dysk HDD SATA 7200 obr. 1000 GB</br>Miejsce na dodatkowy wewnętrzny dysk SATA Możliwość montażu dwóch dysków SATA",
"https://cdn.x-kom.pl/i/setup/images/prod/big/product-big,dell-inspiron-3670-i7-87008gb1281000win10-gtx1050ti-447395,2018/9/pr_2018_9_20_15_45_5_770_00.jpg",
123,
4299,
},
}
}
func Initialize() {
initializePages()
initializeData()
initializePages()
for k := range Pages {
http.HandleFunc(Pages[k].Path, Pages[k].HandlePage)
}

View File

@ -12,8 +12,8 @@ type Page struct {
}
func (p Page) HandlePage(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles(p.Template)
t.Execute(w, p.Data)
t, _ := template.ParseFiles(p.Template, "templates/base.html")
t.ExecuteTemplate(w, "base", p.Data)
}
type Category struct {
@ -34,3 +34,14 @@ type Product struct {
var Pages map[string]Page
var Categories []Category
var Products []Product
func GetCategoryByName(name string) Category {
var category Category
for i:= range Categories {
if Categories[i].Name == name {
category = Categories[i]
break
}
}
return category
}

16
templates/base.html Normal file
View File

@ -0,0 +1,16 @@
{{define "base"}}
<!DOCTYPE html>
<html>
<head>
<title>Elektromarket</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" href="{% static 'scheduyapp/style.css' %}"/>
</head>
<body>
[logo]
{{template "content" .}}
</body>
</html>
{{end}}

View File

@ -1,3 +1,5 @@
[logo]
{{ .test }}
[categories]
{{define "content"}}
{{range $.categories}}
<a href="/category/{{ .Id }}">{{ .Name }}</a>
{{end}}
{{end}}