Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Compiler ¶
type Compiler interface {
// CompileViews parses the registered view sources into the driver's renderer.
CompileViews() error
}
Compiler is implemented by route drivers that compile the registered view sources into their renderer. The framework calls CompileViews once every service provider has booted, so the compiled set contains every view and function registered during Boot regardless of provider order. Implementations must be idempotent: the driver is asked to compile again on its first serve call and must neither parse twice nor lose its renderer.
type Template ¶
type Template interface {
// Data returns a shallow copy of the data passed to the template, without the shared data.
Data() map[string]any
// Name returns the name of the view, or "" when First matched no view.
Name() string
// Render renders the view and returns the resulting HTML. Views are resolved the same way
// as for HTTP responses: application views first, then directories registered with
// LoadViewsFrom, then filesystems registered with LoadViewsFromFS. The compiled set is kept
// until a new view source or template function is registered.
//
// Rendering happens outside a request, so request-bound values such as csrf_token are not
// available.
Render() (string, error)
// With adds a key-value pair to the template data.
With(key string, value any) Template
}
Template is a view bound to its data, ready to be rendered. A template collects data as it is built and is not safe for use from several goroutines at once; render it where it was built, or build one per goroutine.
type View ¶
type View interface {
// Exists checks if a view with the specified name exists.
Exists(view string) bool
// First returns a template for the first view in the list that can be rendered, which is
// not always a view that Exists: a file holding only define blocks under other names exists
// but cannot be rendered by its path. If none of them can be rendered, rendering the
// returned template fails.
First(views []string, data ...any) Template
// LoadViewsFrom registers a package view directory for template fallback.
// Templates from registered directories are loaded after app views; if a
// template name is already defined by an app view or an earlier package, it is skipped.
LoadViewsFrom(path string)
// LoadViewsFromFS registers an fs.FS (for example an embed.FS) as a package view source.
// Templates are resolved relative to root within fsys; pass "." to use the whole filesystem.
// Root may use either slash style ("views/admin" or `views\admin`) and leading "./" or "/"
// are ignored. Filesystem sources are searched after app views and after directories
// registered with LoadViewsFrom, in registration order.
//
// Unlike LoadViewsFrom, which accepts a directory that may only exist later, an fs.FS is
// fixed at build time, so it panics if fsys is nil or root is not an existing directory
// within fsys (including roots that escape it, such as "../views").
//
// //go:embed views
// var views embed.FS
//
LoadViewsFromFS(fsys fs.FS, root string)
// Make returns a template for the view. The data may be a map with string keys or a struct, or
// a pointer to either; nil means no data. It is copied when the template is built, so later
// changes to the caller's map are not seen, and it is merged over the shared data when the
// template is rendered.
Make(view string, data ...any) Template
// RegisteredViews returns the absolute paths of all registered package view directories.
RegisteredViews() []string
// RegisteredViewFS returns all package view filesystems registered with LoadViewsFromFS,
// in registration order. Each returned filesystem is already rooted at the root passed to
// LoadViewsFromFS, so template paths are relative to it (for example "layouts/app.tmpl").
RegisteredViewFS() []fs.FS
// with the current view context. This shared data can be accessed by other parts of the application.
Share(key string, value any)
// If the key does not exist, it returns the optional default value (if provided).
Shared(key string, def ...any) any
GetShared() map[string]any
// AddFuncs registers template functions that the route driver installs on the
// template set before parsing. Functions registered later override earlier ones
// with the same name. The application's functions, supplied through
// ApplicationBuilder.WithViewFuncs, are added after every service provider has
// booted and therefore take precedence over functions registered by packages.
AddFuncs(funcs template.FuncMap)
// Funcs returns a copy of every registered template function.
Funcs() template.FuncMap
}