Code Monkey home page Code Monkey logo

gorilla-controllers's Introduction

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",
    "./templates/sidebar.html",
    "./templates/navbar.html",
    "./templates/footer.html",
}

r := mux.NewRouter()
g := gorillacontrollers.New(r, baseTemplates, "layout") // "layout" is your base template


g.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

gorilla-controllers's People

Contributors

joegasewicz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gorilla-controllers's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.