Skip to content

Commit

Permalink
render funcs in layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Jan 24, 2024
1 parent 40d0434 commit 4d58f55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
3 changes: 3 additions & 0 deletions builder_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ var _ = Describe("Builder", func() {
</head>
<body>
{{.RenderedPage}}
{{range $doc := iterDocs "posts/" 3}}
{{end}}
</body>
</html>
`)
Expand Down
21 changes: 12 additions & 9 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/microcosm-cc/bluemonday"
cp "github.com/otiai10/copy"
"github.com/sabloger/sitemap-generator/smg"
"github.com/samber/lo"
"github.com/tdewolff/minify"
mHTML "github.com/tdewolff/minify/html"
"github.com/yuin/goldmark"
Expand Down Expand Up @@ -104,11 +103,6 @@ func (r *Render) Execute(
return fmt.Errorf("could not read layout: %w", err)
}

layout, err := template.New(r.layoutPath).Parse(contents)
if err != nil {
return fmt.Errorf("could not parse layout template (%s): %w", r.layoutPath, err)
}

funcMap := template.FuncMap{
"iterDocs": func(path string, limit int) (Docs, error) {
pattern := filepath.Join(r.sourcePath, path, "*.md")
Expand All @@ -121,6 +115,15 @@ func (r *Render) Execute(
},
}

layout, err := template.
New(r.layoutPath).
Funcs(funcMap).
Funcs(sprig.FuncMap()).
Parse(contents)
if err != nil {
return fmt.Errorf("could not parse layout template (%s): %w", r.layoutPath, err)
}

maxRenders := 10
group := &errgroup.Group{}

Expand Down Expand Up @@ -316,12 +319,12 @@ func (r *Render) renderDocument(doc *Doc, funcMap template.FuncMap, layout *temp

layoutWriter := &bytes.Buffer{}

err = layout.Execute(layoutWriter, lo.Assign(
funcMap,
err = layout.Execute(layoutWriter,
map[string]any{
"Doc": doc,
"RenderedPage": renderedMarkdown,
}))
},
)
if err != nil {
return fmt.Errorf("could not render layout template (%s): %w", doc.Filename(), err)
}
Expand Down

0 comments on commit 4d58f55

Please sign in to comment.