https://github.com/joegasewicz/gorilla-controllers
Use controllers with the Gorilla Mux library
https://github.com/joegasewicz/gorilla-controllers
controllers gorilla gorilla-mux mvc templates
Last synced: 28 days ago
JSON representation
Use controllers with the Gorilla Mux library
- Host: GitHub
- URL: https://github.com/joegasewicz/gorilla-controllers
- Owner: joegasewicz
- License: mit
- Created: 2021-09-26T17:25:03.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-11-19T11:48:29.000Z (over 3 years ago)
- Last Synced: 2025-04-10T13:34:12.703Z (28 days ago)
- Topics: controllers, gorilla, gorilla-mux, mvc, templates
- Language: Go
- Homepage: https://pkg.go.dev/github.com/joegasewicz/gorilla-controllers
- Size: 27.3 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Gorilla Controllers
Use controllers with the Gorilla Mux library#### Why controllers?
Gorilla's mux library is a brilliant fully featured mux tool, Gorilla Controllers is a library that replaces the HandleFunc
with a `Controller` function & a `Templates` function. This way you only need to manage your data inside your controller,
all your template setup logic is now handled by Gorilla Controllers.### Basic Usage
Create a controller
```go
// Create a controller - template data needs to be passed by value to `data *map[string]interface{}`
func Home(w http.ResponseWriter, r *http.Request, data *map[string]interface{}) {
var templateData map[string]interface{} // Create a map to store your template data
templateData = make(map[string]interface{})
templateData["heading"] = "Create a new advert"
*data = templateData // pass by value back to `data`
}
```
Library Setup
```go
baseTemplates := []string{
"./templates/layout.html",
"./templates/sidebar.html",
"./templates/navbar.html",
"./templates/footer.html",
}r := mux.NewRouter()
g := gorillacontrollers.New(r, baseTemplates, "layout") // "layout" is your base templateg.Route("/")
.Controller(Home) // Controller now replaces Gorilla's HandleFunc
.Methods("GET", "POST")
.Templates("./templates/navbar.html", "./templates/home.html") // If you do not call Templates() then you must call Init() instead```