Share day-style render context and template blocks

This commit is contained in:
2026-06-16 15:03:35 +00:00
parent e3bcecc5c1
commit 121f28fd29
10 changed files with 426 additions and 252 deletions

View File

@@ -8,7 +8,7 @@ import (
"text/template"
)
//go:embed templates/*.md.tmpl schemas/*.schema.json
//go:embed templates/*.md.tmpl templates/partials/*.md.tmpl schemas/*.schema.json
var assets embed.FS
var templates = map[string]string{
@@ -18,6 +18,12 @@ var templates = map[string]string{
"tomorrow": "templates/tomorrow.md.tmpl",
}
var templatePartials = []string{
"templates/partials/daypart_forecast.md.tmpl",
"templates/partials/precipitation_timing.md.tmpl",
"templates/partials/today_daypart_forecast.md.tmpl",
}
var schemas = map[string]string{
"daily": "schemas/daily.generated_text.schema.json",
"hourly": "schemas/hourly.generated_text.schema.json",
@@ -58,6 +64,16 @@ func Render(id string, data any) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("parse report template %q: %w", id, err)
}
for _, path := range templatePartials {
partial, err := assets.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read report template partial %q: %w", path, err)
}
tmpl, err = tmpl.Parse(string(partial))
if err != nil {
return nil, fmt.Errorf("parse report template partial %q: %w", path, err)
}
}
var out bytes.Buffer
if err := tmpl.Execute(&out, data); err != nil {
return nil, fmt.Errorf("render report template %q: %w", id, err)