美文网首页go
搞一个自娱自乐的博客(四) 友链

搞一个自娱自乐的博客(四) 友链

作者: 都是毛线 | 来源:发表于2021-07-07 20:24 被阅读0次

功能描述

将首页改造为友链,功能包含链接标题和链接 因为后台管理系统还没选好 暂时不做添加功能 由数据库添加数据,页面做展示使用。

后台代码

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}}

结语

友链就是这样了 以后找个服务器部署下

相关文章

网友评论

    本文标题:搞一个自娱自乐的博客(四) 友链

    本文链接:https://www.haomeiwen.com/subject/nojaultx.html