Skip to content

Instantly share code, notes, and snippets.

@kingwill101
Last active December 26, 2015 16:13
Show Gist options
  • Save kingwill101/f6babf4ae7961881c7f4 to your computer and use it in GitHub Desktop.
Save kingwill101/f6babf4ae7961881c7f4 to your computer and use it in GitHub Desktop.
/*
[kingwill101@Science-Dept gopress]$ go run main.go
panic: template: home.gtpl:58: function "sayHello" not defined
goroutine 1 [running]:
html/template.Must(0x0, 0x7f02443b4000, 0xc820169820, 0x7f02443b4000)
/usr/local/go/src/html/template/template.go:330 +0x4b
org/gopress/gopress.init()
/home/kingwill101/Documents/GO/src/org/gopress/gopress/handlers.go:9 +0xdb
main.init()
/home/kingwill101/Documents/GO/src/org/gopress/main.go:9 +0x3b
goroutine 17 [syscall, locked to thread]:
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1696 +0x1
exit status 2
*/
type GoPress struct {
currentUser User
db gorm.DB
loggedIn bool
router *mux.Router
gp_helper Helper
templates *template.Template
}
func (g *GoPress) Run() {
g.initDB()
g.routerInit()
}
func (gp *GoPress) routerInit() {
gp.register_tpl_functions()
gp.router = mux.NewRouter()
gp.router.HandleFunc("/", gp.home).Methods("GET")
gp.router.HandleFunc("/register", gp.registration).Methods("POST", "GET")
gp.router.HandleFunc("/gp-panel", gp.adminPanel).Methods("POST", "GET")
gp.router.HandleFunc("/gp-login", gp.adminLogin).Methods("POST", "GET")
gp.router.HandleFunc("/gp-logout", gp.adminLogOut)
assets := http.StripPrefix(DEFAULT__ASSET_URI, http.FileServer(http.Dir(DEFAULT_THEME_STATIC)))
gp.router.PathPrefix(DEFAULT__ASSET_URI).Handler(assets)
assets_admin := http.StripPrefix(ADMIN_ASSET_URI, http.FileServer(http.Dir(ADMIN_STATIC)))
gp.router.PathPrefix(ADMIN_ASSET_URI).Handler(assets_admin)
log.Printf("%s: %s\n", "theme-directory", DEFAULT_THEME_STATIC)
http.Handle("/", gp.router)
http.ListenAndServe(":8100", nil)
}
func (gp *GoPress) home(w http.ResponseWriter, r *http.Request) {
var err error
// tpl.ExecuteTemplate(os.Stdout, "index.html", tplVars)
//home.gtpl register.gtpl login.gtpl
gp.templates = template.Must(template.New("home.gtpl").Funcs(gp.gp_helper.GetFuncs()).ParseGlob(DEFAULT_THEME + "/templates/*"))
err = gp.templates.ExecuteTemplate(w, "home.gtpl", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
//helper struct
func (gp *GoPress) url_for(path string) string {
return "path"
}
func (gp *GoPress) PrintHello() string {
return "Hello"
}
func (gp *GoPress) Assets_admin(path string) string {
return ADMIN_ASSET_URI + path
}
func (gp *GoPress) Assets_default(path string) string {
return ADMIN_ASSET_URI + path
}
type Helper struct {
tpl_functs template.FuncMap
}
func (h *Helper) Init() {
h.tpl_functs = template.FuncMap{}
}
func (h *Helper) AddFunc(key string, _func interface{}) {
h.tpl_functs[key] = _func
}
func (h *Helper) RemoveFunc(key string) {
delete(h.tpl_functs, key)
}
func (h *Helper) GetFuncs() template.FuncMap {
return h.tpl_functs
}
//Functions to be used in template files
func (gp *GoPress) register_tpl_functions() {
gp.gp_helper.Init()
gp.gp_helper.AddFunc("admin_assets", gp.Assets_admin)
gp.gp_helper.AddFunc("theme_assets", gp.Assets_default)
gp.gp_helper.AddFunc("sayHello", gp.PrintHello)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment