contour

package module
v0.0.0-...-02431a1 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2022 License: MIT Imports: 12 Imported by: 0

README

contour

Tiny library for handling templating in a Go web app, batteries included.

Features

  • Layouts
  • Flash messages
  • Auto-reloading (useful in development)

Usage

Initialize and integrate contour like so:

package main

import (
	"net/http"
	"github.com/bobek-balinek/contour"
)

var tmpl *contour.Engine

func init() {
	tmpl = contour.New("./views", ".html")
}

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", index)

	port := ":3000"
	log.Println("Listening on port ", port)
	http.ListenAndServe(port, mux)
}

func index(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/html")
	w.WriteHeader(http.StatusOK)

	tmpl.Render(w, "index", map[string]interface{}{
		"Title": "Hello World",
	}, "layouts/default")

	if _, err := w.Write([]byte(out)); err != nil {
		w.Write([]byte(err.Error()))
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine struct {
	Flashes   *Flash
	Templates *template.Template
	// contains filtered or unexported fields
}

Engine defines the template engine

func New

func New(dir, ext string) *Engine

New returns a new template engine with a default configuration

func NewFS

func NewFS(fs http.FileSystem, ext string) *Engine

NewFS returns a template engine with a default configuration and given file system

func (*Engine) AddFunc

func (e *Engine) AddFunc(name string, fn interface{}) *Engine

AddFunc adds the function to the template's function map. It is legal to overwrite elements of the default actions

func (*Engine) AddFuncMap

func (e *Engine) AddFuncMap(m map[string]interface{}) *Engine

AddFuncMap adds the functions from a map to the template's function map. It is legal to overwrite elements of the default actions

func (*Engine) Layout

func (e *Engine) Layout(key string) *Engine

Layout sets a new method to embed a template within a layout

func (*Engine) Load

func (e *Engine) Load() error

Load parses the templates to the engine.

func (*Engine) Reload

func (e *Engine) Reload(enabled bool) *Engine

Reload if set to true the templates are reloading on each render, use it when you're in development and you don't want to restart the application when you edit a template file.

func (*Engine) Render

func (e *Engine) Render(w io.Writer, name string, data interface{}, layout ...string) error

Render renders a template with given name using provided data and layout template

type Flash

type Flash struct {
	// contains filtered or unexported fields
}

Flash defines a flash message container

func NewFlash

func NewFlash() *Flash

NewFlash returns a new flash message container

func (*Flash) All

func (f *Flash) All() []string

All returns all messages regardless of their type

func (*Flash) Clear

func (f *Flash) Clear()

Clear clears all messages

func (*Flash) Get

func (f *Flash) Get(key string) []string

Get returns all messages of given type

func (*Flash) Push

func (f *Flash) Push(msg string)

Push pushes a new message onto the stack with default type `info`

func (*Flash) PushTo

func (f *Flash) PushTo(key, msg string)

PushTo pushes a new message onto the stack with given type i.e. `info`, `error`, or `success`

Jump to

Keyboard shortcuts

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