all repos — navani @ 8836f20495e17361d15b1da6eb7bc4c6f2cd063c

forlater's primary mail processing service

mail/template.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
package mail

import (
	"bytes"
	"html/template"
	"path/filepath"

	"git.icyphox.sh/forlater/navani/reader"
)

func RenderTemplate(file string, article *reader.Article) ([]byte, error) {
	t, err := template.ParseGlob(filepath.Join("templates", "*.tpl"))
	if err != nil {
		return nil, err
	}

	buf := &bytes.Buffer{}
	if err := t.ExecuteTemplate(buf, file, struct {
		Content template.HTML
		Title   string
		Byline  string
		URL     string
	}{
		template.HTML(article.Content),
		article.Title,
		article.Byline,
		article.URL.String(),
	}); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}