1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 10:54:46 +01:00
gojekyll/layout.go

66 lines
1.3 KiB
Go
Raw Normal View History

package gojekyll
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
2017-06-17 18:31:03 +02:00
"github.com/osteele/gojekyll/liquid"
)
// FindLayout returns a template for the named layout.
2017-06-18 11:59:12 +02:00
func (s *Site) FindLayout(base string, fm *VariableMap) (t liquid.Template, err error) {
exts := []string{"", ".html"}
for _, ext := range strings.SplitN(s.config.MarkdownExt, `,`, -1) {
exts = append(exts, "."+ext)
}
var (
2017-06-17 02:06:55 +02:00
name string
content []byte
found bool
)
for _, ext := range exts {
// TODO respect layout config
name = filepath.Join(s.LayoutsDir(), base+ext)
2017-06-17 02:06:55 +02:00
content, err = ioutil.ReadFile(name)
if err == nil {
found = true
break
}
if !os.IsNotExist(err) {
return nil, err
}
}
if !found {
2017-06-17 02:06:55 +02:00
panic(fmt.Errorf("no template for %s", base))
}
*fm, err = readFrontMatter(&content)
if err != nil {
return
}
return s.LiquidEngine().Parse(content)
}
2017-06-19 04:26:49 +02:00
func (page *DynamicPage) applyLayout(frontMatter VariableMap, body []byte) ([]byte, error) {
for {
2017-06-19 04:26:49 +02:00
name := frontMatter.String("layout", "")
if name == "" {
return body, nil
}
2017-06-19 04:26:49 +02:00
template, err := page.site.FindLayout(name, &frontMatter)
if err != nil {
return nil, err
}
2017-06-19 04:26:49 +02:00
vars := MergeVariableMaps(page.TemplateVariables(), VariableMap{
"content": string(body),
"layout": frontMatter,
})
2017-06-18 11:59:12 +02:00
body, err = template.Render(vars)
if err != nil {
return nil, err
}
}
}