1
0
Fork 0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-05-19 09:06:03 +02:00

Implement support for per-page templates

Implements: https://todo.sr.ht/~adnano/kiln/28
This commit is contained in:
Adnan Maolood 2022-10-01 15:11:57 -04:00
parent 84e9ee61e3
commit e47e3c2790
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*
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"`
Weight int `yaml:"weight"`
Outputs []string `yaml:"outputs"`
Template string `yaml:"template"`
Params map[string]interface{} `yaml:"params"`
Path string `yaml:"-"`
FilePath string `yaml:"-"`
@ -193,7 +194,11 @@ func (p *Page) process(cfg *Site, task *Task) error {
if task.TemplateExt != "" {
// Create 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 {
var b strings.Builder
if err := tmpl.Execute(&b, p); err != nil {
@ -205,8 +210,12 @@ func (p *Page) process(cfg *Site, task *Task) error {
// Process pages
for i := range p.Pages {
tmplName := p.Pages[i].Template
if tmplName == "" {
tmplName = "page"
}
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 err := tmpl.Execute(&b, p.Pages[i]); err != nil {
return err