package lazyview

import "golazy.dev/lazyview"

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

A Views value owns an fs.FS containing templates, the template engines that can render those files, and the helpers available during rendering. New requires the conventional app layout at "layouts/app.html.tpl" because Render defaults to wrapping HTML views in the "app" layout when UseLayout is true. Render looks for controller views such as "posts/index.html.tpl", falls back to app-wide views such as "app/index.html.tpl", and then renders the selected layout with the rendered view body available as the "content" variable.

Template engines are opt-in. Import golazy.dev/lazyview/gotmpl, usually as a blank import, to register the .tpl engine backed by html/template. Other engines can call RegisterEngine for their own file extensions.

Helpers are registered globally with AddHelpers or Helper and may also be supplied per render operation with Options.Helpers. A helper whose first argument is *Context receives request-local render state after the engine binds it for the current render. The built-in "partial" helper renders partial templates named with a leading underscore, such as "posts/_summary.html.tpl", without a layout.

Fragment is rendered markup that compatible engines can embed without treating it as plain text. The gotmpl engine converts Fragment values to trusted template content according to their content type. lazyview uses this for layout content and partials; lazyapp also uses fragments for cache and Turbo helpers.

In a conventional GoLazy application, lazyapp creates one Views value from the embedded application views, registers helpers from lazyroutes, lazyassets, lazyforms, lazyseo, lazyturbo, cache helpers, and application Config.Helpers, then stores the renderer in context through lazycontroller. lazycontroller.Base passes route metadata, controller variables, request helpers, selected format, variants, and layout choices to lazyview when a controller calls Render. lazymailer uses the same renderer from context to render mailer templates and layouts without serving an HTTP request.

Use lazyview directly for standalone rendering, custom controller stacks, pre-rendered strings, or packages that need the same view filesystem and helpers outside lazyapp.

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 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
	// Data is the value used as dot while executing the current template.
	Data	any

	Namespace	string
	Controller	string
	Action		string
	Partial		string
	Format		string
	Variants	[]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
func (c *Context) Helpers

Helpers returns a copy of the unbound helpers for nested render operations.

func (c *Context) Helpers() 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 Options

Options configures one render operation.

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

	Variables	map[string]any
	// Data overrides the value used as dot while executing the template.
	Data	any
	Helpers	map[string]any
	Route	Route

	Namespace	string
	Controller	string
	Action		string
	Partial		string
	Format		string
	Variants	[]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.