loadr

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 8 Imported by: 0

README

loadr

Currently in development, API can still change slightly

loadr is a library which extends the functionality of the standard html/template functionality by providing:

  1. No extra build step
  2. Compile time type safety through the use of generics
  3. All templates are parsed, validated and cached on application startup, fail-fast behaviour
  4. Optional live reload capability (like VSCode's live server), any changes to watched files automatically refreshes the browser
  5. Simplifies layout, partials and component based templating
  6. Uses std lib html/templates under the hood
  7. No performance penalty compared to std lib, check micro-benchmarks in _examples

The library draws inspiration from this article and essentially solidifies the idea into a re-usable pattern across projects.

Install

go get github.com/nesbyte/loadr

Examples

See _examples for more complete and involved examples

About

The philosophy of loadr is to be robust and stable for web development with the goal of becoming "finished", introducing minimal abstractions and opinions. It builds on native Go templating, regular HTML, and the standard library's HTTP library.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleReload added in v0.3.0

func HandleReload(e fsnotify.Event, err error)

A basic helper function for LiveReload to perform logging when a reload occurs

func HandleReloadError added in v0.3.0

func HandleReloadError(e fsnotify.Event, err error)

A basic helper function for LiveReload to perform logging if a reload error occurs

func LoadTemplates added in v0.3.0

func LoadTemplates() error

Loads and validates all the created templates. It is expected to be called after all the templates and settings have been created

func NewBaseTemplate added in v0.3.0

func NewBaseTemplate[T any](baseData T) *core.BaseTemplates[T]

Creates a new base template with the provided baseData.

All NewTemplate calls will use BaseTemplates as their starting point.

The baseData is used to define the data type passed in to the template for the base data for all child templates.

func NewHeaders added in v0.3.0

func NewHeaders() handlers.HeaderSetter

Shorthand to make it easier to create middleware to set headers and does not depend on loadr.LoadTemplates()

Use Set(key, value), to append key values to the header ResponseWriter followed by Middleware() to return a handler middleware

func NewTemplate added in v0.3.0

func NewTemplate[T, U any](br *core.BaseTemplates[T], pattern string, data U) *core.Templ[T, U]

Lazily prepares the templates for loading by pattern both file names as well as the template names can be provided. If no template name is provided, the template name will be the first name of the SetTemplates() pattern

The expected data structure should also be provided as it is used for the loading and validation when loadr.LoadTemplates() is called.

No templates get parsed until loadr.Validate() is run

func RunLiveReload added in v0.3.0

func RunLiveReload(handlePattern string, handleReload func(fsnotify.Event, error), pathsToWatch ...string) (http.HandlerFunc, context.CancelFunc, error)

Watches the specified local pathsToWatch for file changes and notifies connected clients and handleChange if provided.

Live reload can only be started once.

The handlePattern is the URL path that the live server will handle and must match the registered pattern in the HTTP server. If no handlePatern is provided, the live server will serve on /live-server

Types

type BaseConfig added in v0.3.0

type BaseConfig = core.BaseConfig

Used to set the configuration for the base templates

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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