github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/render/template_helpers.go (about) 1 package render 2 3 import ( 4 "encoding/json" 5 "html/template" 6 "path/filepath" 7 8 ht "github.com/gobuffalo/helpers/tags" 9 "github.com/gobuffalo/tags/v3" 10 ) 11 12 type helperTag struct { 13 name string 14 fn func(string, tags.Options) template.HTML 15 } 16 17 func (s templateRenderer) addAssetsHelpers(helpers Helpers) Helpers { 18 helpers["assetPath"] = s.assetPath 19 20 ah := []helperTag{ 21 {"javascriptTag", ht.JS}, 22 {"stylesheetTag", ht.CSS}, 23 {"imgTag", ht.Img}, 24 } 25 26 for _, h := range ah { 27 func(h helperTag) { 28 helpers[h.name] = func(file string, options tags.Options) (template.HTML, error) { 29 if options == nil { 30 options = tags.Options{} 31 } 32 f, err := s.assetPath(file) 33 if err != nil { 34 return "", err 35 } 36 return h.fn(f, options), nil 37 } 38 }(h) 39 } 40 41 return helpers 42 } 43 44 var assetMap = stringMap{} 45 46 func assetPathFor(file string) string { 47 filePath, ok := assetMap.Load(file) 48 if filePath == "" || !ok { 49 filePath = file 50 } 51 return filepath.ToSlash(filepath.Join("/assets", filePath)) 52 } 53 54 func loadManifest(manifest string) error { 55 m := map[string]string{} 56 57 err := json.Unmarshal([]byte(manifest), &m) 58 if err != nil { 59 return err 60 } 61 for k, v := range m { 62 assetMap.Store(k, v) 63 } 64 return nil 65 }