template.go (1549B)
1 package main 2 3 import ( 4 "notabug.org/gearsix/suti" 5 "os" 6 "path/filepath" 7 "strings" 8 ) 9 10 // LoadTemplateDir loads all files in `dir` that are not directories as a `suti.Template` 11 // by calling `suti.LoadTemplateFile`. Partials for each template will be parsed from all 12 // files in a directory matching the base filename of the template (not including 13 // extension) if it exists. 14 func LoadTemplateDir(dir string) (templates []suti.Template, err error) { 15 templatePaths := make(map[string][]string) // map[rootPath][]partialPaths... 16 17 err = filepath.Walk(dir, func(path string, info os.FileInfo, e error) error { 18 lang := strings.TrimPrefix(filepath.Ext(path), ".") 19 if e != nil || info.IsDir() || strings.Contains(path, ".ignore") || suti.IsSupportedTemplateLang(lang) == -1 { 20 return e 21 } 22 23 templatePaths[path] = make([]string, 0) 24 return e 25 }) 26 27 err = filepath.Walk(dir, func(path string, info os.FileInfo, e error) error { 28 lang := strings.TrimPrefix(filepath.Ext(path), ".") 29 if e != nil || info.IsDir() || ignoreFile(path) || suti.IsSupportedTemplateLang(lang) == -1 { 30 return e 31 } 32 33 for t := range templatePaths { 34 if strings.Contains(path, filepath.Dir(t)) && 35 filepath.Ext(t) == filepath.Ext(path) { 36 templatePaths[t] = append(templatePaths[t], path) 37 } 38 } 39 return e 40 }) 41 42 if err == nil { 43 var t suti.Template 44 for rootPath, partialPaths := range templatePaths { 45 t, err = suti.LoadTemplateFilepath(rootPath, partialPaths...) 46 if err != nil { 47 break 48 } 49 templates = append(templates, t) 50 } 51 } 52 53 return 54 }