This is a creation in Article, where the information may have evolved or changed.
1. Design the archive page
First of all, before I start, I want to say that some students may find the import when it is done, one will be "mytest/app/models", one will be "gblog/app/models" This is my fault, no description, I completed the Gblog project, Now the tutorial is to take mytest this for everyone to do, so change the front to your project name on the line, not with me. Why should I specifically say that, I am afraid some students, do when the wrong, and can not find the reason, oneself clearly follow the tutorial why is wrong? This could have hit his interest. So here's a little bit of explanation. Originally I want to go back to change, after all, it will not take long time, but after all, some problems still need to solve their own, so it is explained here.
OK, let's do the archive page, create a new history.html under Views/app, content:
{{set.} "title" "History Blogs-gblog"}}{{set. "History" "Active"}}{{template "header.html".}} <div class= "Content" > <div class= "History-nav" id= "his" > <div class= "history-title" > Blogs Archive </div> <div class= "History-cell" > <div class= "panel-heading" style= "Padding:0;borde r-bottom:1px dashed #ccc; " > <a data-toggle= "collapse" data-toggle= "collapse" data-parent= "#his" href= "#collapseOne" >2014</A&G T </div> <div id= "Collapseone" class= "Panel-collapse collapse in" > <div class= "panel-body "Style=" padding:0 20px; " > <ul style= "padding:10px 10px;list-style:none;" > <li><time>2014-04-01</time><a href= "#" >abourt the Blog</a><span class = "History-auth" >by jov</span></li> </ul> </div> </div> </div> </div></dIv>{{template "Footer.html".}}
To add a method in Controllers/app.go:
Func (c App) history () Revel. Result {return C.render ()}
OK, add our route below, Conf/routes:
GET /history app.history
Look at the effect:
Let's implement it and create a new history.go content inside models:
Package Modelsimport ("Github.com/revel/revel", "Labix.org/v2/mgo/bson" "Time") type history struct {year Intblogs []blog }func (DAO *dao) inserthistory (history *history) Error {historycollection: = Dao.session.DB (DbName). C (historycollection) Err: = Historycollection.insert (History) If err! = Nil {Revel. WARN. Printf ("Unable to save the history:%v error%v", history, Err)}return Err}func (dao *dao) findhistory () []history{historycoll Ection: = Dao.session.DB (DbName). C (historycollection) His: = []history{}query: = Historycollection.find (Bson. m{}). Sort ("-year") query. All (&his) return His}func (DAO *dao) RemoveAll () Error{historycollection: = Dao.session.DB (DbName). C (historycollection) _,err: = Historycollection.removeall (Bson. m{}) If err! = Nil {Revel. WARN. Printf ("Unable to removeall:error%v", err)}return Err}func (dao *dao) createallhistory () {DAO. RemoveAll (); var end int = time. Now (). Year (); for i:=baseyear;i<=end;i++{history: = new (History); Year = I;dao. Inserthistory (History);}}
This model has only two properties, but only the year attribute saved to the DB, look at the last method, we judge the difference between the current and the baseyear based on Baseyear, and then do the archive.
Well, in Controllers/app.go's history method, make the following changes:
Func (c App) history () Revel. Result {DAO, err: = models. Newdao () if err! = Nil {c.response.status = 500return c.rendererror (err)}defer DAO. Close () Dao. Createallhistory (); Historys: = Dao. Findhistory (); for i,_: = Range historys{historys[i]. Blogs =dao. Findblogsbyyear (Historys[i]. year);} Return C.render (Historys)}
Here is another way to query the blog by year, below we add the method in Models/blog.go:
Func (DAO *dao) findblogsbyyear (year int) []blog{blogcollection: = Dao.session.DB (DbName). C (blogcollection) Blogs: = []blog{}query: = Blogcollection.find (Bson. m{"Year": Year}). Sort ("-cdate") query. All (&blogs) return blogs}
OK, modify our page, views/app/history.html:
{{set.} "title" "History Blogs-gblog"}}{{set. "History" "Active"}}{{template "header.html".}} <div class= "Content" > <div class= "History-nav" id= "his" > <div class= "history-title" > Blogs Archive </div> {if. Historys}} {{range $index, $history: =.historys}} <div class= "History-cell "> <div class=" panel-heading "style=" padding:0;border-bottom:1px dashed #ccc; " > <a data-toggle= "collapse" data-toggle= "collapse" data-parent= "#his" href= "#collapseOne {{$index}}" >{{ $history. year}}</a> </div> <div id= "collapseone{{$index}}" class= "Panel-collapse collapse in" > <div class= "Panel-body" style= "padding:0 20px; " > <ul style= "padding:10px 10px;list-style:none;" > {{if $history. Blogs}} {{range $blog: = $history. Blogs}} <li><time>{{$blog. Cdate.format "2006-01-02"}}</time><a Href= "#" >{{$blog. Getshorttitle}}</a><span class= "History-auth" >by {{$blog. email}}</span></li> {{End}} {{end}} </ul> </div > </div> </div> {{end}} {{end}} </div></div>{{template "footer.ht ml ".}}
Okay, OK, let's see the results:
Are you OK now?
Source Address:Https://github.com/joveth/GBlog
AC qq:158325682