搞一个自娱自乐的博客(四) 友链
功能描述
将首页改造为友链,功能包含链接标题和链接 因为后台管理系统还没选好 暂时不做添加功能 由数据库添加数据,页面做展示使用。
后台代码
models
title 标题结构体
package models
type Title struct {
Id int `form:"id"`
Name string `form:"name"`
}
link 存放具体链接
package models
type Link struct {
Id int `form:"id"`
Pid int `form:"pid"`
Title string `form:"title"`
Path string `form:"path"`
}
repo
title_repo 链接数据库查询所有标题
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
}
}
link_repo 链接数据库查询所有链接
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 调用repo查询所有标题
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 调用repo查询所有链接
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 注册linkService和titleSerivce 查询所有标题和链接返回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 service.TitleService
}
// Get 返回message
func (c *LinkController) Get() mvc.Result {
// 获取所有留言
linkList := c.LinkService.GetAll()
titleList := c.TitleService.GetAll()
return mvc.View{
Name: "link.html",
Data: iris.Map{
"Title": titleList,
"Link": linkList,
},
}
}
路由
在route文件夹中增加route.go
package route
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
"myCommunity/controllers"
"myCommunity/service"
)
func Route(app *iris.Application) {
// 注册服务
mvc.Configure(app.Party("/link"), func(app *mvc.Application) {
app.Register(service.NewLinkService())
app.Register(service.NewTitleService())
app.Handle(new(controllers.LinkController))
})
}
main.go中注册路由
// 注册路由
route.Route(app)
前端代码
前端只写主要代码 如下
{{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
遍历对象 以及 unescaped
, unescaped
目的为不转义html代码。go template本身没有这种语法,所以需要增加一个自定义标签。在utils里增加一个文件htmlengine.go
,具体代码如下:
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)
})
}
这样在html里就可以直接使用了 使用方法为 {{unescaped $.item}}
结语
友链就是这样了 以后找个服务器部署下
推荐阅读
「多图预警」完美实现一个@功能
一天产品大大向 boss 汇报完研发成果和产品业绩产出,若有所思的走出来,劲直向我走过来,嘴角微微上扬。产品大大:boss 对我们的研发成果挺满意的,balabala...(内心 OS:不听,讲重点)产品大大:咱们的客服 I...
wuwhs赞 39阅读 4.7k评论 5
在前端使用 JS 进行分类汇总
最近遇到一些同学在问 JS 中进行数据统计的问题。虽然数据统计一般会在数据库中进行,但是后端遇到需要使用程序来进行统计的情况也非常多。.NET 就为了对内存数据和数据库数据进行统一地数据处理,发明了 LINQ (L...
边城赞 17阅读 1.8k
涨姿势了,有意思的气泡 Loading 效果
今日,群友提问,如何实现这么一个 Loading 效果:这个确实有点意思,但是这是 CSS 能够完成的?没错,这个效果中的核心气泡效果,其实借助 CSS 中的滤镜,能够比较轻松的实现,就是所需的元素可能多点。参考我们...
chokcoco赞 18阅读 2k评论 2
你可能不需要JS!CSS实现一个计时器
CSS现在可不仅仅只是改一个颜色这么简单,还可以做很多交互,比如做一个功能齐全的计时器?样式上并不复杂,主要是几个交互的地方数字时钟的变化开始、暂停操作重置操作如何仅使用 CSS 来实现这样的功能呢?一起...
XboxYan赞 20阅读 1.5k评论 1
「彻底弄懂」this全面解析
当一个函数被调用时,会创建一个活动记录(有时候也称为执行上下文)。这个记录会包含函数在 哪里被调用(调用栈)、函数的调用方法、传入的参数等信息。this就是记录的其中一个属性,会在 函数执行的过程中用到...
wuwhs赞 17阅读 2.3k
用了那么久的 SVG,你还没有入门吗?
其实在大部分的项目中都有 直接 或 间接 使用到 SVG 和 Canvas,但是在大多数时候我们只是选择 简单了解 或 直接跳过,这有问题吗?没有问题,毕竟砖还是要搬的!
熊的猫赞 16阅读 1.5k评论 2
学会这些 Web API 使你的开发效率翻倍
随着浏览器的日益壮大,浏览器自带的功能也随着增多,在 Web 开发过程中,我们经常会使用一些 Web API 增加我们的开发效率。本篇文章主要选取了一些有趣且有用的 Web API 进行介绍,并且 API 可以在线运行预览。C...
九旬赞 12阅读 1.5k
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。