binder

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 5 Imported by: 0

README

DOCUMENTATION


gohtmlbinder is a lightweight Go package that makes it easy to bind HTML templates to HTTP routes using Gorilla Mux.
It provides a simple and reusable way to serve web pages without having to manually handle html/template, routes, and servers in every project.

How to use

  • Install the module with:
  go get github.com/Masterpat48/gohtmlbinder
  • Initialize the binder with:
  b := binder.New("file.html")
  • Make a route with static data:
  b.NewRoute("/route", "file.html")
  • Make a route with dynamic data:
  b.NewRouteData("/route", "file.hmtl", func(r *http.Request) any{dynamic data})
  • Print all the registred routes:
	b.PrintRoutes()
  • Start the server:
  b.serve(":port")

Example

  • Static data
package main

import (
	"github.com/Masterpat48/gohtmlbinder"
)

func main() {
	//initialize the binder W the index file (need to have the index file created)
	b := binder.New("index.html")
	//create the main route
	b.NewRoute("/", "index.html")
	//print in console all the registred routes
	b.PrintRoutes()
	//start the server on port 1000
	b.Serve(":1000")
}
  • Dynamic data
package main

import (
	"net/http"
	"sync"

	"github.com/Masterpat48/gohtmlbinder"
)

func main() {
	b := binder.New("index.html")

	var mu sync.Mutex
	count := 0

	// Main route to show the number
	b.NewRouteData("/", "index.html", func(r *http.Request) any {
		mu.Lock()
		defer mu.Unlock()
		return map[string]any{"Count": count}
	})

	// Route to increment the number
	b.NewRouteData("/increment", "index.html", func(r *http.Request) any {
		if r.Method == http.MethodPost {
			mu.Lock()
			count++
			mu.Unlock()
		}

		mu.Lock()
		defer mu.Unlock()
		return map[string]any{"Count": count}
	})

	b.PrintRoutes()
	b.Serve(":1000")
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Counter Example</title>
</head>
<body>
  <h1>Counter: {{ .Count }}</h1>
  <form action="/increment" method="POST">
    <button type="submit">Increment</button>
  </form>
</body>
</html>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Binder

type Binder struct {
	Router *mux.Router
	// contains filtered or unexported fields
}

func New

func New(baseTemplate string) *Binder

function new used to start the binder baseTempalte can be a file or a pattern like "templates/*html"

func (*Binder) NewRoute

func (b *Binder) NewRoute(path string, templateName string)

function NewRoute used to create a route w static data based on a HTML template

func (*Binder) NewRouteData

func (b *Binder) NewRouteData(path string, templateName string, dataFunc func(*http.Request) any)

function NewRouteData used to create a route w dynamic data

func (*Binder) PrintRoutes added in v0.1.2

func (b *Binder) PrintRoutes()

function to print all the registred routes (to call before Serve)

func (*Binder) Serve

func (b *Binder) Serve(addr string) error

function serve used to start the server with a chosen port

func (*Binder) Status added in v0.1.3

func (b *Binder) Status()

function Status to print the server current status when you access the /status endpoint

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL