Created
October 14, 2012 23:57
-
-
Save jordanorelli/3890204 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func readTemplateFile(abspath string) (*template.Template, error) { | |
relpath, err := filepath.Rel(TemplateRoot, abspath) | |
if err != nil { | |
return nil, fmt.Errorf(`core: unable to resolve template file path %v`, abspath) | |
} | |
t := template.New(relpath).Funcs(template.FuncMap{ | |
"jsonblob": func(v interface{}) (template.JS, error) { | |
b, err := json.Marshal(v) | |
if err != nil { | |
return "", err | |
} | |
return template.JS(b), nil | |
}, | |
}) | |
b, err := ioutil.ReadFile(abspath) | |
if err != nil { | |
return nil, err | |
} | |
s := string(b) | |
t, err = t.Parse(s) | |
if err != nil { | |
return nil, fmt.Errorf(`core: unable to read template file at path %v: %v`, abspath, err) | |
} | |
fi, err := os.Stat(abspath) | |
if err != nil { | |
return nil, fmt.Errorf(`core: unable to stat (2) template at path %v: %v`, abspath, err) | |
} | |
templateCache[relpath] = cachedTemplate{t, fi} | |
return t, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment