Last active
March 17, 2017 14:28
-
-
Save codelitt/549a68149add0482c6dc2514a46aa580 to your computer and use it in GitHub Desktop.
Trying to add cachebusted css file to the ExecuteTemplate method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
imports ( | |
"path/filepath" | |
"strings" | |
"github.com/gorilla/mux" | |
) | |
func main() { | |
server_port := ":3000" | |
r := mux.NewRouter() | |
r.Handle("/", Home).Methods("GET") | |
r.HandleFunc("/signup", usersC.New).Methods("GET") | |
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./assets/"))) | |
http.ListenAndServe(server_port, r) | |
} | |
func NewStatic() *Static { | |
return &Static{ | |
Home: views.NewView("layout", "static/home"), | |
} | |
} | |
type Static struct { | |
Home *views.View | |
} | |
func NewView(layout string, files ...string) *View { | |
files = append(files, layoutFiles()...) | |
t, err := template.ParseFiles(files...) | |
if err != nil { | |
panic(err) | |
} | |
return &View{ | |
Template: t, | |
Layout: layout, | |
} | |
} | |
type View struct { | |
Template *template.Template | |
Layout string | |
} | |
func (v *View) Render(w http.ResponseWriter, data interface{}) error { | |
d := Data{} | |
d.AddCSSFile() | |
w.Header().Set("Content-Type", "text/html") | |
err := v.Template.ExecuteTemplate(w, v.Layout, d) | |
if err != nil { | |
log.Println(err) | |
fmt.Fprintln(w, "<h1>Something went wrong. Please contact us at support") | |
} | |
return nil | |
} | |
func (v *View) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
v.Render(w, nil) | |
} | |
func (u *Users) New(w http.ResponseWriter, r *http.Request) { | |
vd := views.Data{} | |
form := SignupForm{} | |
if err := parseQuery(r, &form); err != nil { | |
vd.AddAlerts(views.AlertGeneric) | |
u.NewView.RenderError(w, vd) | |
return | |
} | |
u.NewView.Render(w, views.Data{Yield: &form}) | |
} | |
type Data struct { | |
Alerts []Alert | |
Yield interface{} | |
CSSFile interface{} | |
} | |
func (d *Data) AddCSSFile() { | |
ss, _ := filepath.Glob("./assets/site-*.css") | |
fp := strings.Join(ss, "") | |
_, d.CSSFile = filepath.Split(fp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, allow me to add one suggestion, expanding a bit more on the stackoverflow discussion.
First, if you know for a fact that the
data
argument passed to theRender
method will, in many cases, be of different types then you can keep doing what you're doing and disregard the rest of this comment.But if the
data
argument you're passing to theRender
method will always be of the typeData
then you should define theRender
method as such that its second argument is not of typeinterface{}
but of typeData
or*Data
. That way you don't have to use type assertion, not that it's a bad thing to use it but you'll always know what you're getting, the compiler will protect you from petty mistakes, you still can callAddCSSFile
insideRender
on thedata Data
argument, and, when in the end, you need to pass some new type toRender
just add a new field to theData
type and you're good togo
.Below is an example of what i mean: