filebrowser/page.go

52 lines
1.0 KiB
Go
Raw Normal View History

2016-06-10 21:18:44 +00:00
package filemanager
import (
2016-06-10 21:38:52 +00:00
"bytes"
"html/template"
"log"
2016-06-10 21:18:44 +00:00
)
type Page struct {
2016-06-10 21:38:52 +00:00
Config *Config
Data interface{}
2016-06-10 21:18:44 +00:00
}
2016-06-10 21:38:52 +00:00
func (f FileManager) formatAsHTML(data interface{}, fmc *Config, templates ...string) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
pg := &Page{
Config: fmc,
Data: data,
}
templates = append(templates, "base")
var tpl *template.Template
// For each template, add it to the the tpl variable
for i, t := range templates {
// Get the template from the assets
page, err := Asset("templates/" + t + ".tmpl")
// Check if there is some error. If so, the template doesn't exist
if err != nil {
log.Print(err)
return new(bytes.Buffer), err
2016-06-10 21:18:44 +00:00
}
2016-06-10 21:38:52 +00:00
// If it's the first iteration, creates a new template and add the
// functions map
if i == 0 {
tpl, err = template.New(t).Parse(string(page))
} else {
tpl, err = tpl.Parse(string(page))
2016-06-10 21:18:44 +00:00
}
2016-06-10 21:38:52 +00:00
if err != nil {
log.Print(err)
return new(bytes.Buffer), err
2016-06-10 21:18:44 +00:00
}
}
2016-06-10 21:38:52 +00:00
err := Template.Execute(buf, pg)
return buf, err
2016-06-10 21:18:44 +00:00
}