package lazyview

import "golazy.dev/lazyview"

Package lazyview provides view rendering, helper registration, and render context handling independent from any concrete template engine.

Functions

func RegisterEngine

RegisterEngine registers a template engine for a file extension.

Extensions are matched without a leading dot. Engine packages normally call this from init, allowing applications to opt into engines with a blank import.

func RegisterEngine(extension string, factory EngineFactory)

Types

type CacheClearer

CacheClearer can discard cached templates when view configuration changes.

type CacheClearer interface {
	ClearCache()
}

type CacheContext

CacheContext is passed to engines that can precompile templates.

type CacheContext struct {
	FS		fs.FS
	Extension	string
	Helpers		map[string]any
}

type Context

Context contains the request-local state used while rendering a view.

type Context struct {
	Context	context.Context
	Request	*http.Request

	Views	*Views
	Route	Route

	Variables	map[string]any

	Namespace	string
	Controller	string
	Action		string
	Partial		string
	Format		string
	Layout		string
	// contains filtered or unexported fields
}
func (c *Context) Helper

Helper returns one helper bound to the current render context.

func (c *Context) Helper(name string) (any, bool)
func (c *Context) HelperFuncs

HelperFuncs returns helper functions bound to the current render context.

func (c *Context) HelperFuncs() map[string]any

type Engine

Engine renders one view file using a concrete template implementation.

type Engine interface {
	Render(ctx *Context, writer io.Writer, file string) error
}

type Fragment

Fragment is rendered output that can be embedded by compatible engines.

type Fragment struct {
	Body		string
	ContentType	string
}

type Helper

Helper is the native context-aware helper function shape.

type Helper func(*Context, ...any) (any, error)

type Options

Options configures one render operation.

type Options struct {
	Context	context.Context
	Request	*http.Request
	Writer	io.Writer

	Variables	map[string]any
	Helpers		map[string]any
	Route		Route

	Namespace	string
	Controller	string
	Action		string
	Partial		string
	Format		string
	Layout		string
	UseLayout	bool
}

type Route

Route is request route metadata made available to renderers and helpers.

type Route struct {
	Name		string
	Method		string
	Path		string
	Namespace	string
	Controller	string
	Action		string
	Params		map[string]string
}

type Views

Views owns the application view filesystem, registered engines, and global helpers.

type Views struct {
	FS	fs.FS
	Engines	map[string]Engine
	Helpers	map[string]any
}
func New

New builds a view set using the engines registered by imported engine packages.

func New(files fs.FS) (*Views, error)
func (v *Views) AddHelpers

AddHelpers adds helpers to the view set.

func (v *Views) AddHelpers(helpers map[string]any)
func (v *Views) Cache

Cache builds or rebuilds template caches for engines that support caching.

Applications should register helpers before calling Cache. If helpers are changed later, AddHelpers clears existing caches and Cache should be called again before serving requests.

func (v *Views) Cache() error
func (v *Views) Helper

Helper adds one helper to the view set.

func (v *Views) Helper(name string, helper any)
func (v *Views) Render

Render renders a view and optionally wraps it with a layout.

func (v *Views) Render(options Options) error
func (v *Views) RenderString

RenderString renders a view to a string.

func (v *Views) RenderString(options Options) (string, error)

Directories

Path Synopsis
lazyview/gotmpl Package gotmpl registers Go's html/template engine for lazyview.