remove unnecessary template type knowledge

This commit is contained in:
facundoolano 2024-02-12 15:33:30 -03:00
parent e97af58830
commit 0f2c2f37aa
3 changed files with 12 additions and 50 deletions

View file

@ -88,17 +88,18 @@ func loadTemplates(site *Site) error {
return filepath.WalkDir(SRC_DIR, func(path string, entry fs.DirEntry, err error) error {
if !entry.IsDir() {
templ, err := templates.Parse(path)
if err != nil {
// if sometime fails or this is not a template, skip
if err != nil || templ == nil {
return err
}
switch templ.Type {
case templates.POST:
// posts are templates that can be chronologically sorted --that have a date.
// the rest are pages.
if _, ok := templ.Metadata["date"]; ok {
site.posts = append(site.posts, *templ)
case templates.PAGE:
} else {
site.pages = append(site.pages, *templ)
}
// TODO add tags
}
return nil
})

View file

@ -19,7 +19,8 @@ type Site struct {
}
func (site Site) render(templ *templates.Template) (string, error) {
ctx := site.contextFor(templ)
ctx := site.baseContext()
ctx["page"] = templ.Metadata
content, err := templ.Render(ctx)
if err != nil {
return "", err
@ -29,6 +30,7 @@ func (site Site) render(templ *templates.Template) (string, error) {
layout := templ.Metadata["layout"]
for layout != nil && err == nil {
if layout_templ, ok := site.layouts[layout.(string)]; ok {
ctx["layout"] = layout_templ.Metadata
ctx["content"] = content
content, err = layout_templ.Render(ctx)
layout = layout_templ.Metadata["layout"]
@ -48,17 +50,10 @@ func (site Site) templateIndex() map[string]*templates.Template {
return templIndex
}
func (site Site) contextFor(templ *templates.Template) map[string]interface{} {
bindings := map[string]interface{}{
func (site Site) baseContext() map[string]interface{} {
return map[string]interface{}{
"config": site.config,
"posts": site.posts,
"tags": site.tags,
}
if templ.Type == templates.LAYOUT {
bindings["layout"] = templ.Metadata
} else {
// assuming that if it's not a layout then it must be a page
bindings["page"] = templ.Metadata
}
return bindings
}

View file

@ -16,35 +16,11 @@ import (
const FM_SEPARATOR = "---"
type Type string
const (
// a file that doesn't have a front matter header, and thus is not renderable.
STATIC Type = "static"
// Templates in the root /layouts/ can be used to wrap around other template's content
// by setting the `layout` front matter field.
LAYOUT Type = "layout"
// A template that has a date, and thus can be ordered chronologically in a directory.
// They can thus be arranged in archives, feeds, etc.
// Posts are also assumed to have a title and can be excerpted.
POST Type = "post"
// The rest of the templates: no layout and no post
PAGE Type = "page"
)
type Template struct {
Type Type
SrcPath string
Metadata map[string]interface{}
}
// TODO think about knowledge boundaries
// should this know to tell if its a layout based on srcPath conventions?
// should it be able to detect its own type? does it still make sense to track a template type,
// separate from the site?
func Parse(path string) (*Template, error) {
file, err := os.Open(path)
if err != nil {
@ -58,7 +34,7 @@ func Parse(path string) (*Template, error) {
// if the file doesn't start with a front matter delimiter, it's not a template
if strings.TrimSpace(line) != FM_SEPARATOR {
return &Template{Type: STATIC}, nil
return nil, nil
}
// read and parse the yaml from the front matter
@ -85,16 +61,6 @@ func Parse(path string) (*Template, error) {
}
templ := Template{SrcPath: path, Metadata: metadata}
// FIXME this also should check that it's in the root folder
if strings.HasSuffix(filepath.Dir(templ.SrcPath), "layouts") {
templ.Type = LAYOUT
} else if _, ok := metadata["date"]; ok {
templ.Type = POST
} else {
templ.Type = PAGE
}
return &templ, nil
}