mark2web/render.go
Sebastian Frank 29f01a2618
All checks were successful
continuous-integration/drone/push Build is passing
reorganized code
2019-03-18 15:14:41 +01:00

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)
}