package lazyview ¶
import "golazy.dev/lazyview"
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 CacheableEngine ¶
CacheableEngine can build or rebuild a template cache after app setup.
type CacheableEngine interface {
Cache(ctx CacheContext) error
}
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 EngineFactory ¶
EngineFactory creates a fresh engine instance for one Views value.
type EngineFactory func() Engine
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
// 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. |
Package lazyview provides view rendering, helper registration, and render context handling independent from any concrete template engine.
lazyapp creates a lazyview.Views value for conventional applications and lazycontroller renders through it. Template engines are registered by importing subpackages such as golazy.dev/lazyview/gotmpl.
Use lazyview directly for custom rendering stacks, non-controller output, or packages such as lazymailer that share the same embedded views and helpers without serving an HTTP request.