dta25920 2016-07-01 07:32
浏览 90
已采纳

如何在HTML文件中编写Golang代码(Gin Gonic框架)

Im newbie in golang code as well as in gin gonic. I got a problem while using gin gonic.

In my controller. i get all articles and render to html file by code.

c.HTML(http.StatusOK, "articles/list", gin.H{
    "title":    "Articles",
    "articles": articles,
})

and articles have field "CreatedOn" type int64 (created date) So in my view list.html, how i can parse CreateOn type int64 to date format.

<div class="list-group">
  {{ range $article := $articles }}
    <a href="/articles/{{ $article.Id }}" class="list-group-item">
      <h4 class="list-group-item-heading">{{ $article.Title }}</h4>
      <p class="list-group-item-text">{{ $article.Body }}</p>
      <p class="list-group-item-text">{{ $article.CreatedOn  }}</p>
      <p class="list-group-item-text"></p>
    </a>
  {{ end }}
  </div>

Thanks all

I had found a way that i write a method FormatDate()

func (a *Article) FormatDate(ab int64) string {
    return "test Time"
}

in model "Article". then in my view i call

  <p class="list-group-item-text">{{ .FormatDate article.CreatedOn  }}</p>

Anything else????

  • 写回答

1条回答 默认 最新

  • doudang4857 2016-10-05 13:49
    关注

    TL;DR use SetHTMLTemplate

    Looking at the Gin documentation, you can use your own templating engine.

    By invoking r.SetHTMLTemplate(tmpl)

    Gin itself is using the golang builtin html/template standard package.

    You can use the same engine and add user defined functions.

    Create the functions, using template.FuncMap:

    funcMap := template.FuncMap{
        "formatTime": func(raw int64) string {
            t := time.Unix(raw, 0)
    
            return t.Format("Jan 2 15:04:05 2006")
        },
    }
    

    Instantiate a template:

    tmpl := template.Must(template.New("main").Funcs(funcMap).ParseGlob("templates/**/*"))
    

    Register the new template:

    r := gin.Default()
    r.SetHTMLTemplate(tmpl)
    

    If you use same templates name for different endpoints, specify name:

    {{ define "articles/list.tmpl"}}
    
    <div class="list-group">
    {{ range $article := .articles }}
      <a href="/articles/{{ $article.Id }}" class="list-group-item">
        <h4 class="list-group-item-heading">{{ $article.Title }}</h4>
        <p class="list-group-item-text">{{ formatTime $article.CreatedOn }}</p>
        <p class="list-group-item-text"></p>
      </a>
    {{ end }}
    </div>
    
    {{ end }}
    

    formatTime: Is defined using template.FuncMap

    To invoke, use the normal way:

    c.HTML(http.StatusOK, "articles/list", gin.H{
        "title":    "Articles",
        "articles": articles,
    }) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格