bou.ke/statictemplate@v0.0.0-20180821122055-510411a5e7dd/dev_template.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io" 6 ) 7 8 func writeDevTemplate(w io.Writer, targets compilationTargets, templateFiles []string, html bool, funcMapImport, funcMapName string, pkg string) error { 9 fmt.Fprintf(w, `// +build dev 10 11 package %s 12 13 import ( 14 "io" 15 `, pkg) 16 if html { 17 io.WriteString(w, `"html/template" 18 `) 19 } else { 20 io.WriteString(w, `"text/template" 21 `) 22 } 23 if funcMapImport != "" { 24 fmt.Fprintf(w, "funcMapImport %q\n", funcMapImport) 25 } 26 for i, target := range targets { 27 if target.dot.packagePath != "" { 28 fmt.Fprintf(w, "pkg%d %q\n", i, target.dot.packagePath) 29 } 30 } 31 io.WriteString(w, ")\n") 32 for i, target := range targets { 33 var dot string 34 if target.dot.packagePath == "" { 35 dot = fmt.Sprintf("%s%s", target.dot.prefix, target.dot.typeName) 36 } else { 37 dot = fmt.Sprintf("%spkg%d.%s", target.dot.prefix, i, target.dot.typeName) 38 } 39 fmt.Fprintf(w, `func %s(w io.Writer, dot %s) error { 40 temp, err := template.New("")`, target.functionName, dot) 41 if funcMapName != "" { 42 fmt.Fprintf(w, ".Funcs(funcMapImport.%s)", funcMapName) 43 } 44 io.WriteString(w, ".ParseFiles(\n") 45 for _, templateFile := range templateFiles { 46 fmt.Fprintf(w, "%q,\n", templateFile) 47 } 48 io.WriteString(w, `) 49 if err != nil { 50 return err 51 } 52 return temp.Execute(w, dot) 53 } 54 `) 55 } 56 return nil 57 }