Add category template, replace uint to int, rename market to main
This commit is contained in:
parent
24ca2bd55f
commit
fe8b875365
@ -7,7 +7,8 @@ import (
|
|||||||
|
|
||||||
func initializePages() {
|
func initializePages() {
|
||||||
Pages = make(map[string]Page)
|
Pages = make(map[string]Page)
|
||||||
Pages["index"] = Page{Path: "/", Template: "templates/index.html", Data: map[string]interface{}{"categories": Categories}}
|
Pages["index"] = Page{"/", "templates/index.html", map[string]interface{}{"categories": Categories}, nil}
|
||||||
|
Pages["category"] = Page{"/category", "templates/category.html", map[string]interface{}{}, DynamicDataLoaderCategory}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeData() {
|
func initializeData() {
|
||||||
|
@ -3,26 +3,41 @@ package models
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Page struct {
|
type Page struct {
|
||||||
Path string
|
Path string
|
||||||
Template string
|
Template string
|
||||||
Data map[string]interface{}
|
Data map[string]interface{}
|
||||||
|
DynamicDataLoader func(int) map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DynamicDataLoaderCategory(id int) map[string]interface{}{
|
||||||
|
return map[string]interface{}{
|
||||||
|
"category": GetCategoryById(id),
|
||||||
|
"products": GetCategoryProducts(id),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Page) HandlePage(w http.ResponseWriter, r *http.Request) {
|
func (p Page) HandlePage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if p.DynamicDataLoader != nil {
|
||||||
|
id, err := strconv.Atoi(r.URL.Query()["id"][0])
|
||||||
|
if err == nil {
|
||||||
|
p.Data = p.DynamicDataLoader(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
t, _ := template.ParseFiles(p.Template, "templates/base.html")
|
t, _ := template.ParseFiles(p.Template, "templates/base.html")
|
||||||
t.ExecuteTemplate(w, "base", p.Data)
|
t.ExecuteTemplate(w, "base", p.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Category struct {
|
type Category struct {
|
||||||
Id uint
|
Id int
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Product struct {
|
type Product struct {
|
||||||
Id uint
|
Id int
|
||||||
Category Category
|
Category Category
|
||||||
Name string
|
Name string
|
||||||
Description string
|
Description string
|
||||||
@ -45,3 +60,25 @@ func GetCategoryByName(name string) Category {
|
|||||||
}
|
}
|
||||||
return category
|
return category
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetCategoryById(id int) Category {
|
||||||
|
var category Category
|
||||||
|
for i:= range Categories {
|
||||||
|
if Categories[i].Id == id {
|
||||||
|
category = Categories[i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return category
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCategoryProducts(id int) []Product {
|
||||||
|
var products []Product
|
||||||
|
for i:= range Products {
|
||||||
|
if Products[i].Category.Id == id {
|
||||||
|
products = append(products, Products[i])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return products
|
||||||
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
[logo]
|
<a href="http://127.0.0.1:8000">[Strona główna]<a/>
|
||||||
{{template "content" .}}
|
{{template "content" .}}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
6
templates/category.html
Normal file
6
templates/category.html
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{{define "content"}}
|
||||||
|
{{.category.Name}}
|
||||||
|
{{range $.products}}
|
||||||
|
<a href="">{{ .Name }}</a>
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
@ -1,5 +1,5 @@
|
|||||||
{{define "content"}}
|
{{define "content"}}
|
||||||
{{range $.categories}}
|
{{range $.categories}}
|
||||||
<a href="/category/{{ .Id }}">{{ .Name }}</a>
|
<a href="/category?id={{ .Id }}">{{ .Name }}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
Loading…
Reference in New Issue
Block a user