Use controllers with the Gorilla Mux library

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

// 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

baseTemplates := []string{
    "./templates/layout.html", // You must always call your base html file "layout"
    "./templates/sidebar.html",
    "./templates/navbar.html",
    "./templates/footer.html",
}

r := mux.NewRouter()
g := gorillacontrollers.New(r, baseTemplates)


g.Route("/")
    .Controller(Home) // Controller now replaces Gorilla's HandleFunc
    .Methods("GET", "POST")
    .Templates("./templates/home.html") // If you do not call Templates() then you must call Init() instead
Owner
Joe Gasewicz
Hi! I'm a Go, Python & Typescript full stack developer. I'm interested in Game dev with Rust & Bevy. For fun I study C, I'm a Jazz bassist & prefer cats...
Joe Gasewicz
Similar Resources

code that I re-use among many Go projects

A bunch of Go packages that I use in multiple projects. An overview of packages: u : utility functions that I use all the time. Very short package nam

Dec 30, 2022

Go 1.18 generics use cases and examples

Go 1.18 generics use cases What are generics? See Type Parameters Proposal. How to run the examples? As of today, gotip is the simplest way to run the

Jan 10, 2022

Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang)

Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang)

Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang). It has support for Windows too! The API can be used in several ways, pick one that suits you.

Feb 5, 2022

Easy to use open source hardware to drive WS2811 LEDs with high-quality color

STOP DOING FADECANDY LEDs were not supposed to be given data pins YEARS of "temporal dithering" but no real-world use found for having more than three

Dec 29, 2022

Govalid is a data validation library that can validate most data types supported by golang

Govalid is a data validation library that can validate most data types supported by golang. Custom validators can be used where the supplied ones are not enough.

Apr 22, 2022

Maintain a lower-bitrate copy of a music library in sync with the main copy.

msync Maintain a lower-bitrate copy of your music library, in sync with the main copy.

Mar 6, 2022

Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs.

go-attr Golang library to act on structure fields at runtime. Similar to Python getattr(), setattr(), hasattr() APIs. This package provides user frien

Dec 16, 2022

Go library for HTTP content type negotiation

Content-Type support library for Go This library can be used to parse the value Content-Type header (if one is present) and select an acceptable media

Jul 10, 2022

A tool and library for using structural regular expressions.

Structural Regular Expressions sregx is a package and tool for using structural regular expressions as described by Rob Pike (link).

Dec 7, 2022
Easy to use, light enough, good performance Golang library
 Easy to use, light enough, good performance Golang library

指令使用 特性 简单易用、足够轻量,避免过多的外部依赖,最低兼容 Window 7 等老系统 快速上手 安装 $ go get github.com/sohaha/zlsgo HTTP 服务 // main.go

Dec 29, 2022
Use is a go utility library using go1.18 generics

use use is a go utility library using go1.18 generics created by halpdesk 2022-01-22 use/slice Map updates a slice by applying a function to all membe

Jan 22, 2022
Helpers for making the use of reflection easier

go-xray This is a Golang library with reflection related functions which I use in my different projects. KeyValue This type is used to construct a key

Oct 24, 2022
Use Golang to implement PHP's common built-in functions.

PHP2Go Use Golang to implement PHP's common built-in functions. About 140+ functions have been implemented. Install go get github.com/syyongx/php2go R

Dec 28, 2022
Robust & Easy to use struct mapper and utility methods for Go

go-model Robust & Easy to use model mapper and utility methods for Go struct. Typical methods increase productivity and make Go development more fun ?

Dec 30, 2022
🚀 Use Lanyard API easily in your Go app!

?? Go Lanyard Use Lanyard API easily in your Go app! ?? Installation Initialize your project (go mod init example.com/example) Add package (go get git

Mar 11, 2022
Go linter to check the struct literal to use field name

Structfield Find struct literals using non-labeled fields. The structfield analysis reports the usage of struct literal using non-labeled fields more

Aug 23, 2021
Infrastructure supporting the use of Go in Microsoft

Microsoft Go Infrastructure This repository contains libraries and utilities that Microsoft uses to: Build Go inside Microsoft infrastructure using up

Dec 15, 2022
DO NOT use `named return values` in Go

namedreturn DO NOT use named return values in Go namedreturn finds a named return value, you must remove it.

Dec 17, 2021
To run a .go file use below syntax

To run a .go file use below syntax

Nov 2, 2021