zoukankan      html  css  js  c++  java
  • [golang] call the struct function in the template range

    here is the code of a struct

    type Post struct {
        Id          int    `db:"id"`
        Title       string `db:"title"`
        Content     string `db:"content"`
        Create_time int64    `db:"create_time"`
    }
    
    func (p Post) AttrCreatetime() time.Time {
        return time.Unix(p.Create_time, 0)
    }
     

    How to call the function "AttrCreatetime" in the template range? 

    That is very simple to achieve it. Here is the code of the html file.

                            {{range .posts}}
                <article class="article ar-in">
                    <div class="ar">
                        <span class="ar-ti">
                            <a href="{{url "Post.View" .Id}}" title="{{.Title}}">{{.Title}}</a>
                        </span>
                        <small>{{.AttrCreatetime}}</small>
                    </div>
                    <div class="ar-ds">
                        {{.Content}}
                        <span class="ar-mr"><a href="{{url "Post.View" .Id}}">more...</a></span>
                    </div>
                </article>
                {{end}}

    See? {{.AttrCreatetime}} is now la!

    Have fun with golang!

  • 相关阅读:
    js模块化历程
    夜深
    出差(六)开会
    高情商的十大典型表现
    出差(五)调整
    HighCharts简单应用
    出差(四)适应
    出差(三)尝试
    出差(二)熟悉
    ZTree简单应用
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/3042151.html
Copyright © 2011-2022 走看看