[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!

posted @ 2013-04-25 11:59  DavidHHuan  阅读(883)  评论(0编辑  收藏  举报