35 lines
872 B
Go
35 lines
872 B
Go
package mark2web
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/flosch/pongo2"
|
|
)
|
|
|
|
var templateCache = make(map[string]*pongo2.Template)
|
|
var templateDir string
|
|
|
|
// SetTemplateDir sets base directory for searching template files
|
|
func SetTemplateDir(dir string) {
|
|
templateDir = dir
|
|
}
|
|
|
|
// renderTemplate renders a pongo2 template with context
|
|
func renderTemplate(filename string, node *TreeNode, pathConfig *PathConfig, ctx *pongo2.Context) (string, error) {
|
|
CurrentContext = ctx
|
|
CurrentTreeNode = node
|
|
CurrentPathConfig = pathConfig
|
|
templateFile := templateDir + "/" + filename
|
|
template := templateCache[templateFile]
|
|
if template == nil {
|
|
var err error
|
|
if template, err = pongo2.FromFile(templateFile); err != nil {
|
|
log.Panicf("could not parse template '%s': %s", templateFile, err)
|
|
} else {
|
|
templateCache[templateFile] = template
|
|
}
|
|
}
|
|
|
|
return template.Execute(*ctx)
|
|
}
|