1.设计归档页面
首先在开始之前,我要说一下,有的同学在做的时候可能发现import的时候,一会是"MyTest/app/models",一会是"GBlog/app/models" 这是我的错,没有说明,我自己完成的是GBlog项目,现在做的教程是拿MyTest这个给大家做的,所以把前面改成你的项目名称就行,不用跟我这一样。为什么要特别说一下呢,我怕有些同学,做的时候出了错,而找不到原因,自己明明跟着教程做的为什么是错的呢。这样可能打击他的兴趣。所以这里说明一下。本来我想回去都改一下的,毕竟也花不了多久时间,但是毕竟有些问题还是需要大家自己解决的,所以才在这里说明。
好下面我们来做归档页面,在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"> 博客归档 </div> <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">2014</a> </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" .}}
在controllers/app.go添加方法:
func (c App) History() revel.Result { return c.Render() }
好,下面添加我们的路由,conf/routes:
GET /history App.History
看一下效果:
下面我们来实现它,在models里面新建history.go 内容:
package models import ( "github.com/revel/revel" "labix.org/v2/mgo/bson" "time" ) type History struct { Year int Blogs []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 History: %v error %v", history, err) } return err } func (dao *Dao) FindHistory() []History{ historyCollection := 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); history.Year = i; dao.InsertHistory(history); } }
这个model只有两个属性,但是保存到db中的只有year属性,看一下最后的方法,我们以BaseYear为基础,判断当前年与baseyear之间的差,然后做归档。
好,在controllers/app.go的History方法中,做如下修改:
func (c App) History() revel.Result { dao, err := models.NewDao() if err != nil { c.Response.Status = 500 return 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) }
这里又多了一个方法,按年查询blog,下面我们在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 }
好的,修改我们的页面,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"> 博客归档 </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.html" .}}
好了,ok,来看看结果:
你是否已经ok了呢?
有疑问加站长微信联系(非本文作者)