func serveAssets(w http.ResponseWriter, r *http.Request) (int, error) { filename := strings.Replace(r.URL.Path, "/admin/", "", 1) file, err := assets.Asset(filename) if err != nil { return 404, nil } // Get the file extension ant its mime type extension := filepath.Ext(filename) mime := mime.TypeByExtension(extension) // Write the header with the Content-Type and write the file // content to the buffer w.Header().Set("Content-Type", mime) w.Write(file) return 200, nil }
// GetTemplate is used to get a ready to use template based on the url and on // other sent templates func GetTemplate(r *http.Request, functions template.FuncMap, templates ...string) (*template.Template, error) { // If this is a pjax request, use the minimal template to send only // the main content if r.Header.Get("X-PJAX") == "true" { templates = append(templates, "base_minimal") } else { templates = append(templates, "base_full") } var tpl *template.Template // For each template, add it to the the tpl variable for i, t := range templates { // Get the template from the assets page, err := assets.Asset("templates/" + t + ".tmpl") // Check if there is some error. If so, the template doesn't exist if err != nil { log.Print(err) return new(template.Template), err } // If it's the first iteration, creates a new template and add the // functions map if i == 0 { tpl, err = template.New(t).Funcs(functions).Parse(string(page)) } else { tpl, err = tpl.Parse(string(page)) } if err != nil { log.Print(err) return new(template.Template), err } } return tpl, nil }