Commit af92fa4d authored by Dustin L. Howett's avatar Dustin L. Howett
Browse files

Abstract out glob loading to provide for file/string loading

parent 71cfc04e
package vast
import "html/template"
// TemplateLoader is an interface that provides template data to a vast Model.
// Template loaders should produce a single output template comprising all
// of the data they can load.
type TemplateLoader interface {
LoadTemplate(baseTemplate *template.Template) (*template.Template, error)
}
type Glob string
func (g Glob) LoadTemplate(baseTemplate *template.Template) (*template.Template, error) {
return baseTemplate.ParseGlob(string(g))
}
type Files []string
func (f Files) LoadTemplate(baseTemplate *template.Template) (*template.Template, error) {
return baseTemplate.ParseFiles(f...)
}
type Strings []string
func (s Strings) LoadTemplate(baseTemplate *template.Template) (*template.Template, error) {
var err error
for _, v := range s {
baseTemplate, err = baseTemplate.Parse(v)
if err != nil {
break
}
}
return baseTemplate, err
}
......@@ -24,7 +24,7 @@ type FunctionProvider interface {
// behavior is documented in the package-level documentation above.
type Model struct {
mu sync.Mutex
glob string
loader TemplateLoader
baseTemplate *template.Template
tmpl *template.Template
bound []*View
......@@ -75,7 +75,7 @@ func (m *Model) Reload() error {
return err
}
tmpl, err = tmpl.ParseGlob(m.glob)
tmpl, err = m.loader.LoadTemplate(tmpl)
if err != nil {
return err
}
......@@ -94,9 +94,9 @@ func (m *Model) Reload() error {
// New returns a new Model bound to the supplied data provider. The data
// provider will be used for all `global` variable lookups.
func New(glob string, options ...ModelOption) (*Model, error) {
func New(loader TemplateLoader, options ...ModelOption) (*Model, error) {
m := &Model{
glob: glob,
loader: loader,
}
tmpl := template.New(".base").Funcs(template.FuncMap{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment