blog/templates/index.go

41 lines
1.1 KiB
Go

// templates/index.go
package templates
import (
"bytes"
"fmt"
"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.
// It accepts a list of posts (already sorted).
func RenderLatestPosts(posts []PostSnippet) string {
var buf bytes.Buffer
buf.WriteString(`<div class="latest-posts"><h2>Latest Posts</h2><ul>`)
for _, p := range posts {
dateStr := p.Date.Format("Jan 02, 2006")
buf.WriteString(fmt.Sprintf(`<li><span class="date">%s</span> <a href="%s">%s</a></li>`, dateStr, p.URL, p.Title))
}
buf.WriteString(`</ul></div>`)
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>`
}
// RenderSiteHeadline generates the top banner.
func RenderSiteHeadline(text string) string {
return fmt.Sprintf(`<div class="site-headline"><h1>%s</h1></div>`, text)
}