Implement support for per-page templates

Implements: https://todo.sr.ht/~adnano/kiln/28
This commit is contained in:
adnano 2022-10-01 15:11:57 -04:00
parent da0af1701c
commit 085ef5aded
2 changed files with 25 additions and 2 deletions

View file

@ -132,6 +132,20 @@ The following keys are supported:
--- ---
``` ```
*template*
Optionally specifies the name of the template to use when building this
page. If unspecified, defaults to "page" for regular pages and "index" for
index pages.
Example:
```
---
title: About me
template: about
---
```
*params* *params*
Specifies extra parameters to be provided to templates. Specifies extra parameters to be provided to templates.

13
page.go
View file

@ -22,6 +22,7 @@ type Page struct {
Date time.Time `yaml:"date"` Date time.Time `yaml:"date"`
Weight int `yaml:"weight"` Weight int `yaml:"weight"`
Outputs []string `yaml:"outputs"` Outputs []string `yaml:"outputs"`
Template string `yaml:"template"`
Params map[string]interface{} `yaml:"params"` Params map[string]interface{} `yaml:"params"`
Path string `yaml:"-"` Path string `yaml:"-"`
FilePath string `yaml:"-"` FilePath string `yaml:"-"`
@ -193,7 +194,11 @@ func (p *Page) process(cfg *Site, task *Task) error {
if task.TemplateExt != "" { if task.TemplateExt != "" {
// Create index // Create index
if p.index { if p.index {
tmpl, ok := cfg.templates.FindTemplate(p.FilePath, "index"+task.TemplateExt) tmplName := p.Template
if tmplName == "" {
tmplName = "index"
}
tmpl, ok := cfg.templates.FindTemplate(p.FilePath, tmplName+task.TemplateExt)
if ok { if ok {
var b strings.Builder var b strings.Builder
if err := tmpl.Execute(&b, p); err != nil { if err := tmpl.Execute(&b, p); err != nil {
@ -205,8 +210,12 @@ func (p *Page) process(cfg *Site, task *Task) error {
// Process pages // Process pages
for i := range p.Pages { for i := range p.Pages {
tmplName := p.Pages[i].Template
if tmplName == "" {
tmplName = "page"
}
var b strings.Builder var b strings.Builder
tmpl, ok := cfg.templates.FindTemplate(p.FilePath, "page"+task.TemplateExt) tmpl, ok := cfg.templates.FindTemplate(p.FilePath, tmplName+task.TemplateExt)
if ok { if ok {
if err := tmpl.Execute(&b, p.Pages[i]); err != nil { if err := tmpl.Execute(&b, p.Pages[i]); err != nil {
return err return err