Functional description
Home page will be transformed into a friend chain, the function includes links title and links because the background management system has not been selected temporarily do not add functions by the database to add data, the page to show the use of.
The background code
models
Title structure
package models
type Title struct {
Id int `form:"id"`
Name string `form:"name"`
}
Link holds concrete links
package models
type Link struct {
Id int `form:"id"`
Pid int `form:"pid"`
Title string `form:"title"`
Path string `form:"path"`
}
repo
TITLE_REPO links to the database to query all titles
package repo import ( "github.com/go-xorm/xorm" "log" "myCommunity/models" ) type TitleRepo struct { engine *xorm.Engine } func TitleDao(engine *xorm.Engine) *TitleRepo { return &TitleRepo{ engine: engine, } } func (repo TitleRepo) GetAll() []models.Title { var datalist []models.Title err := repo.engine.Where("1=1").Find(&datalist) if err ! = nil { log.Println(err) return datalist } else { return datalist } }
The link_repo link database queries all links
package repo import ( "github.com/go-xorm/xorm" "log" "myCommunity/models" ) type LinkRepo struct { engine *xorm.Engine } func LinkDao(engine *xorm.Engine) *LinkRepo { return &LinkRepo{ engine: engine, } } func (repo LinkRepo) GetAll() []models.Link { var datalist []models.Link err := repo.engine.Where("1=1").Find(&datalist) if err ! = nil { log.Println(err) return datalist } else { return datalist } }
service
TITLE_SERVICE calls repo to look up all titles
package service
import (
"myCommunity/datasource"
"myCommunity/models"
"myCommunity/repo"
)
type TitleService interface {
GetAll() []models.Title
}
func NewTitleService() *titleService {
return &titleService{
dao: repo.TitleDao(datasource.DbHelper()),
}
}
type titleService struct {
dao *repo.TitleRepo
}
func (b titleService) GetAll() []models.Title {
return b.dao.GetAll()
}
Link_service calls repo to query for all links
package service import ( "myCommunity/datasource" "myCommunity/models" "myCommunity/repo" ) type LinkService interface { GetAll() []models.Link } func NewLinkService() *linkService { return &linkService{ dao: repo.LinkDao(datasource.DbHelper()), } } type linkService struct { dao *repo.LinkRepo } func (b linkService) GetAll() []models.Link { return b.dao.GetAll() }
controller
Link_controller registers LinkService and TitleServce to query all titles and links back to link.html
package controllers import ( "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/mvc" "myCommunity/service" ) Type linkController struct {linkService service.LinkService titleService.TitleService} // Get returns message func (c * linkController) Get() mvc.result {// Get() mvc.result { return mvc.View{ Name: "link.html", Data: iris.Map{ "Title": titleList, "Link": linkList, }, } }
routing
Add route.go to the route. route folder
package route import ( "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/mvc" "myCommunity/controllers" "MyCommunity /service") func Route(app *iris.Application) {// Configure(app.party ("/link"), func(app *mvc.Application) { app.Register(service.NewLinkService()) app.Register(service.NewTitleService()) app.Handle(new(controllers.LinkController)) }) }
Register the route in main.go
// register route route. route (app)
The front-end code
The front end only writes the main code as follows
{{range $d := .Title}}
<article>
<section>
<h1>{{$d.Name}}</h1>
<p>
{{range $l := $.Link}}
{{if eq $l.Pid $d.Id }}
<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="show" href="{{unescaped $l.Path}}" target="_blank">
<i class="layui-icon layui-icon-list"></i>{{$l.Title}}
</a>
{{end}}
{{end}}
</p>
</section>
</article>
{{end}}
Range traversing objects is used and Unescaped is used to avoid escaping HTML code. The Go Template itself does not have this syntax, so you need to add a custom tag. Add a file htmlengine.go to utils with the following code:
package utils import ( "github.com/kataras/iris/v12/view" "html/template" ) func HtmlEngine(html *view.HTMLEngine) { // HTML HTML.AddFunc("unescaped", func(STR string) template.html {return template.html (STR)})}
{{unescaped $.item}} {{unescaped $.item}}
conclusion
Friend chain is like this later find a server to deploy