A simple collection of things for writing web stuff.
Docs and examples are avaiable at godoc
A quick example, straight from the docs (the later is always up-to-date):
-
The code
package main import ( "net/http" "github.com/urandom/webfw" "github.com/urandom/webfw/context" ) type Hello struct { webfw.BasePatternController } func NewHello(pattern string) Hello { return Hello{webfw.NewBasePatternController(pattern, MethodAll, "")} } func (con Hello) Handler(c *context.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { params := webfw.GetParams(c, r) d := renderer.RenderData{"name": params["name"]} err := webfw.GetRenderCtx(c, r)(w, d, "hello.tmpl") if err != nil { webfw.GetLogger(c, r).Print(err) } } } func Example() { s := webfw.NewServer() dispatcher := s.Dispatcher("/") dispatcher.Handle(NewHello("/hello/:name")) if err := s.ListenAndServe(); err != nil { panic(err) } } func main() { Example() }
-
The templates, in a directory "templates"
2.1 "base.tmpl":
<!doctype html> <html> <body>{{ template "content" . }}</body> </html> {{ define "content" }}{{ end }}
2.2 "hello.tmpl"
{{ define "content" }} <h1>Hello {{ .name }}</h1> {{ end }}