blog/templates/index.go

44 lines
977 B
Go

// templates/index.go
package templates
import (
"bytes"
"html/template"
"time"
)
// PostSnippet represents data needed for the index list
type PostSnippet struct {
Title string
URL string
Date time.Time
}
// RenderLatestPosts generates the HTML for the list of recent posts.
func RenderLatestPosts(posts []PostSnippet) string {
const tpl = `<div class="latest-posts">
<h2>Latest Posts</h2>
<ul>
{{range .}}
<li><span class="date">{{.Date.Format "Jan 02, 2006"}}</span> <a href="{{.URL}}">{{.Title}}</a></li>
{{end}}
</ul>
</div>`
t := template.Must(template.New("latest").Parse(tpl))
var buf bytes.Buffer
if err := t.Execute(&buf, posts); err != nil {
return ""
}
return buf.String()
}
// RenderDirectoryLink generates the navigation link to the full archive.
func RenderDirectoryLink() string {
return `<div class="directory-link"><a href="/archive">View All Posts</a></div>`
}
func RenderTopBanner(content string) string {
return content
}