dqkyz02602 2018-12-22 18:15
浏览 25
已采纳

在Struct中添加一些日期,然后将其放入模板

I have a file controllers/catalog.go it contains a HTTP handler:

func Catalog(w http.ResponseWriter, r *http.Request) {
    if r.Method != "GET" {
        http.Error(w, http.StatusText(405), http.StatusMethodNotAllowed)
        return
    }

    categories, err := models.GetCategories()
    if err != nil {
        http.Error(w, http.StatusText(500), http.StatusInternalServerError)
        return
    }
    fmt.Print(categories)
    config.TPL.ExecuteTemplate(w, "catalog.html", categories)
}

and models/getcategories.go:

type Cat_tree struct {
    Cat_id    int
    Parent_id int
    Cat_name  string
}

func GetCategories() ([]Cat_tree, error) {
    rows, err := config.DB.Query("SELECT cat_id, parent_id, cat_name FROM categories WHERE active = true ORDER BY Parent_id ASC")
    if err != nil {
        return nil, err
    }
    defer rows.Close()

    categories := make([]Cat_tree, 0)

    for rows.Next() {
        cat := Cat_tree{}
        err := rows.Scan(&cat.Cat_id, &cat.Parent_id, &cat.Cat_name)
        if err != nil {
            return nil, err
        }
        categories = append(categories, cat)
    }
    if err = rows.Err(); err != nil {
        return nil, err
    }

    return categories, nil
}

How i can add some data to a categories page Title for example

Now in template like this

   {{range .}}
    <p><a href="/show?getinfo={{ .Cat_id}}">{{ .Cat_id}}</a> - {{ .Parent_id}} - {{ .Cat_name}} <a href="/show?getinfo={{ .Cat_id}}">Показать</a>
    {{end}}

i'd like to add some {{Title}}

  • 写回答

2条回答 默认 最新

  • donglin9068 2018-12-22 18:57
    关注

    You have to create another struct and put categories and page info in there. you can use maps too, but struct is cleaner approach.

    type Page struct {
    Title string
    Categories []Cat_Tree
    }
    
    func Catalog(w http.ResponseWriter, r *http.Request) {
        if r.Method != "GET" {
            http.Error(w, http.StatusText(405), http.StatusMethodNotAllowed)
            return
        }
    
        categories, err := models.GetCategories()
        if err != nil {
            http.Error(w, http.StatusText(500), http.StatusInternalServerError)
            return
        }
        page := Page {Title:"My Title",Categories:categories}
        config.TPL.ExecuteTemplate(w, "catalog.html", page)
    }
    

    And in your template:

    <h1>{{ .Title }}</h1>
    {{range .Categories}}
        <p><a href="/show?getinfo={{ .Cat_id}}">{{ .Cat_id}}</a> - {{ .Parent_id}} - {{ .Cat_name}} <a href="/show?getinfo={{ .Cat_id}}">Показать</a>
        {{end}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题