Categorygithub.com/rwirdemann/simpleweb
modulepackage
0.1.1
Repository: https://github.com/rwirdemann/simpleweb.git
Documentation: pkg.go.dev

# README

SimpleWeb

A very simple library for building serverside rendered web apps in Golang. Example:

import (
    "embed"
    "github.com/rwirdemann/simpleweb"
    "net/http"
)

// Expects all HTML templates in $PROJECTROOT/templates
//
//go:embed all:templates
var templates embed.FS

func init() {
    // Required Init call to tell SimpleWeb about its embedded templates, list 
    // of base templates (empty) and port
    simpleweb.Init(templates, []string{}, 3030)
}

func main() {
    simpleweb.Register("/", func(w http.ResponseWriter, r *http.Request) {
        simpleweb.Render("templates/index.html", w, struct {
            Name string
        }{Name: "SimpleWeb"})
    })
    simpleweb.Run()
}

All required artefacts like static or dynamic HTML pages are embedded. The compiled application consist of one single binary without any external dependency on the target machine.

Basic Templating

The central rendering function simpleweb.Render expects a template name together with an anonymous data struct:

simpleweb.Render("templates/index.html", w, struct {
    Name string
}{Name: "SimpleWeb"})

The data attributes are referred inside the template via a dot prefix, e.g.:

<h1>Hello, {{.Name}}</h1>

Layouting

Avoid repetition of base HTML components by providing a base layout that is rendered with every request:

<!-- templates/layout.html -->
<!DOCTYPE html>
<html lang="en">
<head>
   ...
</head>
<body>
<h1>Header</h1>
{{template "_content" .}}
<h1>Footer</h1>
</body>
</html>

In order to render templates/layout.html with every request it needs to be passed as baseTemplates-param to the initial Init-call:

simpleweb.Init(templates, []string{"templates/layout.html"}, 3030)

The template-tag between the header and footer-elements is replaced by the content file passed to render:

simpleweb.Render("templates/index.html", w, struct {
    Name string
}{Name: "SimpleWeb"})

It is also required that the content is wrapped inside a defined block, thus templates/index.html should have the following format:

<!-- templates/index.html -->
{{define "_content"}}
<p>Hello, {{.Name}}</p>
{{end}}

Flash messages

Flash messages are one-time messages that are rendered by the handler that generates the final HTML and are deleted afterward:

f := func(w http.ResponseWriter, r *http.Request) {
    simpleweb.Info("info message")
    simpleweb.Render("templates/index.html", w, struct {
        Name string
    }{Name: "SimpleWeb"})
}

View helpers are provided for info, warning and error messages. See hasInfo and info for showing info messages:

{{if hasInfo}}
<p style="color: green">{{info}}</p>
{{end}}

Limitations

Since SimpleWeb uses go:embed it is required that all templates are accessible from the projects root directory. Thus, if your project root is hello all templates must live in hello or in one of its subdirectories.

# Packages

No description provided by the author

# Functions

No description provided by the author
FormValue returns the value of key for POST and PUT requests.
No description provided by the author
Init initializes the webapp by providing a collection of embedded html templates fs, the names of baseTemplates that are rendered with every request (e.g.
RedirectE redirects to url after setting Flash.error to err.
No description provided by the author
Render renders tmpl using the provided data.
RenderE renders tmpl using the provided data and returns.
RenderPartialE renders the partial without any surrounding templates.
Run starts the web server on the given port.
No description provided by the author
Static registers path as root directory for serving static content like css, js or images.
No description provided by the author