Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions contracts/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import "io/fs"
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.
Expand All @@ -23,6 +28,9 @@ type View interface {
// 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;
// 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,
Expand All @@ -38,3 +46,22 @@ type View interface {
// GetShared returns a map containing all the shared data associated with the current view context.
GetShared() map[string]any
}

// 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 Template interface {
// Data returns a copy of the data passed to the template, without the shared data.
Data() map[string]any
// Name returns the name of the view.
Name() string
// Render renders the view and returns the resulting HTML. Templates are resolved the same
// way as for HTTP responses: application views first, then directories registered with
// LoadViewsFrom, then filesystems registered with LoadViewsFromFS.
//
// 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
}
2 changes: 2 additions & 0 deletions errors/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ var (

ViewFSRequired = New("view filesystem is required")
ViewFSRootNotDirectory = New("view filesystem root %q is not a directory")
ViewInvalidData = New("view %s data must be a map with string keys or a struct, got %T")
ViewInvalidFSRoot = New("invalid view filesystem root %q: %v")
ViewNoneExist = New("none of the views %v exist")
ViewTemplateNotExist = New("view template %s does not exist")
)
231 changes: 231 additions & 0 deletions mocks/view/Template.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading