Ich bin Neuling in Golang-Code sowie in Gin gonic. Ich habe ein Problem mit Gin gonic.Wie man golang code in html-datei schreibt (gin gonic framework)
In meinem Controller. Ich bekomme alle Artikel und rendern HTML-Datei nach Code.
c.HTML(http.StatusOK, "articles/list", gin.H{
"title": "Articles",
"articles": articles,
})
und Artikel haben Feld „CreatedOn“ Typ int64 (Erstellungsdatum) Also meiner Ansicht list.html, wie ich CreateOn Typ int64 zu Datumsformat analysieren kann.
<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>
Dank all
ich eine Möglichkeit, dass ich eine Methode Format schreiben gefunden hatte()
func (a *Article) FormatDate(ab int64) string {
return "test Time"
}
in Modell "Artikel". dann aus meiner Sicht rufe ich
<p class="list-group-item-text">{{ .FormatDate article.CreatedOn }}</p>
Alles andere ????
Ist das nicht Ihr 'Format()' Ansatz zu arbeiten? – Nadh
natürlich hat es funktioniert. Es ist eine Methode, ich möchte eine Hilfsfunktion für die Vorlage erstellen, aber ich weiß nicht, wie ich es aufrufen kann – Vutuz