github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/template/template.go (about) 1 package template 2 3 import ( 4 "crypto/md5" 5 "errors" 6 "fmt" 7 "html/template" 8 "io" 9 "strings" 10 11 "github.com/GeertJohan/go.rice" 12 ) 13 14 // ErrTemplateNotFound indicates the requested template 15 // does not exists in the TemplateStore. 16 var ErrTemplateNotFound = errors.New("Template Not Found") 17 18 // registry stores a map of Templates where the key 19 // is the template name and the value is the *template.Template. 20 var registry = map[string]*template.Template{} 21 22 // ExecuteTemplate applies the template associated with t that has 23 // the given name to the specified data object and writes the output to wr. 24 func ExecuteTemplate(wr io.Writer, name string, data interface{}) error { 25 templ, ok := registry[name] 26 if !ok { 27 return ErrTemplateNotFound 28 } 29 30 return templ.ExecuteTemplate(wr, "_", data) 31 } 32 33 // all template are loaded on initialization. 34 func init() { 35 // location of templates 36 box := rice.MustFindBox("pages") 37 38 // these are all the files we need to parse. it is 39 // kind of annoying that we can't list files in the 40 // box, and have to enumerate each file here, but it is 41 // a small price to pay to embed everything and simplify 42 // the user installation process :) 43 var files = []string{ 44 // these templates use the form.html 45 // shared layout 46 "login.html", 47 "login_error.html", 48 "forgot.html", 49 "forgot_sent.html", 50 "reset.html", 51 "signup.html", 52 "register.html", 53 "install.html", 54 55 // these templates use the default.html 56 // shared layout 57 "403.html", 58 "404.html", 59 "500.html", 60 "user_dashboard.html", 61 "user_password.html", 62 "user_profile.html", 63 "user_delete.html", 64 "user_teams.html", 65 "user_teams_add.html", 66 "team_dashboard.html", 67 "team_profile.html", 68 "team_members.html", 69 "team_delete.html", 70 "members_add.html", 71 "members_edit.html", 72 "repo_dashboard.html", 73 "repo_settings.html", 74 "repo_delete.html", 75 "repo_params.html", 76 "repo_badges.html", 77 "repo_keys.html", 78 "repo_commit.html", 79 "admin_users.html", 80 "admin_users_edit.html", 81 "admin_users_add.html", 82 "admin_settings.html", 83 "github_add.html", 84 "github_link.html", 85 "gitlab_add.html", 86 "gitlab_link.html", 87 "bitbucket_add.html", 88 "bitbucket_link.html", 89 } 90 91 // extract the base template as a string 92 base, err := box.String("base.html") 93 if err != nil { 94 panic(err) 95 } 96 97 assets := rice.MustFindBox("../../cmd/droned/assets") 98 mainjs, err := assets.String("js/main.js") 99 if err != nil { 100 panic(err) 101 } 102 103 h := md5.New() 104 io.WriteString(h, mainjs) 105 jshash := fmt.Sprintf("%x", h.Sum(nil)) 106 base = strings.Replace(base, "main.js", "main.js?h="+jshash, 1) 107 108 // extract the base form template as a string 109 form, err := box.String("form.html") 110 if err != nil { 111 panic(err) 112 } 113 114 // loop through files and create templates 115 for i, file := range files { 116 // extract template from box 117 page, err := box.String(file) 118 if err != nil { 119 panic(err) 120 } 121 122 // HACK: choose which base template to use FOR THE RECORD I 123 // don't really like this, but it works for now. 124 var baseTemplate = base 125 if i < 8 { 126 baseTemplate = form 127 } 128 129 // parse the template and then add to the global map 130 baseParsed, err := template.New("_").Parse(baseTemplate) 131 if err != nil { 132 panic(fmt.Errorf("Error parsing base.html template: %s", err)) 133 } 134 pageParsed, err := baseParsed.Parse(page) 135 if err != nil { 136 panic(fmt.Errorf("Error parsing page template for %s: %s", file, err)) 137 } 138 139 registry[file] = pageParsed 140 } 141 142 // location of templates 143 box = rice.MustFindBox("emails") 144 145 files = []string{ 146 "activation.html", 147 "failure.html", 148 "success.html", 149 "invitation.html", 150 "reset_password.html", 151 } 152 153 // extract the base template as a string 154 base, err = box.String("base_email.html") 155 if err != nil { 156 panic(err) 157 } 158 159 // loop through files and create templates 160 for _, file := range files { 161 // extract template from box 162 email, err := box.String(file) 163 if err != nil { 164 panic(err) 165 } 166 baseParsed, err := template.New("_").Parse(base) 167 if err != nil { 168 panic(fmt.Errorf("Error parsing base_email.html template: %s", err)) 169 } 170 emailParsed, err := baseParsed.Parse(email) 171 if err != nil { 172 panic(fmt.Errorf("Error parsing email template for %s: %s", file, err)) 173 } 174 175 // parse the template and then add to the global map 176 registry[file] = emailParsed 177 } 178 }