html tempalte works!
This commit is contained in:
parent
4d05a266d1
commit
6ccf2a6898
38
news/news.go
38
news/news.go
|
@ -22,6 +22,7 @@ type News struct {
|
||||||
type NewsContainer struct {
|
type NewsContainer struct {
|
||||||
Name string
|
Name string
|
||||||
News []News
|
News []News
|
||||||
|
Category *categories.Category
|
||||||
Children map[int]*NewsContainer
|
Children map[int]*NewsContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,6 +43,20 @@ func nullStringToString(str *sql.NullString) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addContainer(category_id int, flat, tree map[int]*NewsContainer) {
|
||||||
|
container := &NewsContainer{ Category: categories.CategoriesFlat[category_id], Name: categories.CategoriesFlat[category_id].Name, News: []News{}, Children: map[int]*NewsContainer{} }
|
||||||
|
flat[category_id] = container
|
||||||
|
parent := categories.CategoriesFlat[category_id].Parent
|
||||||
|
if parent.Valid {
|
||||||
|
if _, ok := flat[int(parent.Int64)]; !ok {
|
||||||
|
addContainer(int(parent.Int64), flat, tree)
|
||||||
|
}
|
||||||
|
flat[int(parent.Int64)].Children[category_id] = container
|
||||||
|
} else {
|
||||||
|
tree[category_id] = container
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Unexported(db *sql.DB) (map[int]*NewsContainer, error) {
|
func Unexported(db *sql.DB) (map[int]*NewsContainer, error) {
|
||||||
categories.LoadCategories(db)
|
categories.LoadCategories(db)
|
||||||
|
|
||||||
|
@ -78,25 +93,16 @@ func Unexported(db *sql.DB) (map[int]*NewsContainer, error) {
|
||||||
cid = int(category_id.Int64)
|
cid = int(category_id.Int64)
|
||||||
}
|
}
|
||||||
|
|
||||||
var container *NewsContainer
|
if _, ok := newsFlat[cid]; !ok {
|
||||||
var ok bool
|
addContainer(cid, newsFlat, newsTree)
|
||||||
fmt.Println("cid: ", cid)
|
|
||||||
if container, ok = newsFlat[cid]; !ok {
|
|
||||||
fmt.Println("ADDING")
|
|
||||||
// need new container for new cid
|
|
||||||
container = &NewsContainer{ Name: categories.CategoriesFlat[cid].Name, News: []News{}, Children: map[int]*NewsContainer{} }
|
|
||||||
newsFlat[cid] = container
|
|
||||||
parent := categories.CategoriesFlat[cid].Parent
|
|
||||||
if parent.Valid {
|
|
||||||
fmt.Println("parent: ", parent.Int64)
|
|
||||||
newsFlat[int(parent.Int64)].Children[cid] = container
|
|
||||||
} else {
|
|
||||||
fmt.Println("no parent")
|
|
||||||
newsTree[cid] = container
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
container := newsFlat[cid]
|
||||||
container.News = append(container.News, news)
|
container.News = append(container.News, news)
|
||||||
}
|
}
|
||||||
|
|
||||||
return newsTree, nil
|
return newsTree, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *NewsContainer) HeaderDepth(start int) int {
|
||||||
|
return start + this.Category.Depth()
|
||||||
}
|
}
|
|
@ -13,6 +13,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"github.com/dballard/transmet/categories"
|
"github.com/dballard/transmet/categories"
|
||||||
"github.com/dballard/transmet/news"
|
"github.com/dballard/transmet/news"
|
||||||
|
"bytes"
|
||||||
|
"html/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetFlashes(session *sessions.Session) map[string]interface{} {
|
func GetFlashes(session *sessions.Session) map[string]interface{} {
|
||||||
|
@ -195,8 +197,18 @@ func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var templateBuf bytes.Buffer
|
||||||
|
template, err := template.ParseFiles("templates/html_template.html")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error processing html_tempalte:" , err)
|
||||||
|
}
|
||||||
|
err = template.Execute(&templateBuf, map[string]interface{}{"news": news})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Exec err: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "news": news})
|
ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes, "template": &templateBuf})
|
||||||
}
|
}
|
||||||
|
|
||||||
func templatePostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
func templatePostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
{{range $nc := .news}}{{template "news-category" $nc}}{{end}}
|
||||||
|
{{define "news-category"}}<h{{$.HeaderDepth 1 }}>{{$.Name}}</h{{$.HeaderDepth 1 }}>
|
||||||
|
{{range $n := .News}}{{template "news" $n}}{{end}}{{range $nc := .Children}}{{template "news-category" $nc}}{{end}}{{end}}
|
||||||
|
{{define "news"}}<p><a href="{{.Url}}">{{.Title}}</a><br/>
|
||||||
|
{{.Notes}}</p>
|
||||||
|
{{end}}
|
|
@ -10,6 +10,6 @@
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<textarea class="form-control" name="template" placeholder="Template" rows="3" cols="80">{{.template}}</textarea>
|
<textarea class="form-control" name="template" placeholder="Template" rows="16" cols="80">{{.template}}</textarea>
|
||||||
|
|
||||||
{{end}}
|
{{end}}
|
Loading…
Reference in New Issue