Documentation
¶
Overview ¶
Package view renders HTML templates from resources/views and provides the built-in template functions available to every view.
In an HTTP handler, return a view response with web.View or render a template to HTML bytes directly with Render. Call Exists to check whether a template is defined and Share in middleware to expose request-scoped data to layouts:
name := "plans." + plan.Slug
if !view.Exists(r, name) {
name = "plans.default"
}
html, err := view.Render(r, name, plan)
Each template function reads the request or application state it needs from the render context, so templates call helpers without passing extra arguments:
<form method="POST" action="{{route "posts.update" "post" .Post.ID}}">
{{csrfField}}
{{methodField "PUT"}}
<input name="title" value="{{old "title" .Post.Title}}">
{{with error "title"}}<p class="error">{{.}}</p>{{end}}
</form>
Request-dependent helpers such as csrfField, old, error, session, auth, and head work when rendering a view for an HTTP request. Rendering them in a mail view or background job returns an error. Application-scoped helpers such as route, url, config, vite, viteAsset, and vendorAsset work in any view.
Formatting and list helpers take options first and the value last so they compose cleanly in template pipelines:
<h2>{{.Title | title}}</h2>
<time>{{date "Jan 2, 15:04" .PublishedAt}}</time> · {{forHumans .CreatedAt}}
<footer>© {{date "2006" now}}</footer>
<p>{{.Tags | join ", "}}</p>
<p>{{.Summary | default "No summary yet."}}</p>
<p>Total: {{sum "Total" .Orders | round 2}}</p>
{{range groupBy "Status" .Orders}}<h3>{{.Key}}</h3>{{len .Items}}{{end}}
{{dump .}}
See Funcs for the complete template function map and VendorAsset for resolving versioned package asset URLs from Go code.
Index ¶
- func Exists(ctx context.Context, name string) bool
- func Funcs(ctx context.Context) template.FuncMap
- func Render(ctx context.Context, name string, data any) ([]byte, error)
- func Share(r *web.Request, key string, value any) *web.Request
- func VendorAsset(ctx context.Context, namespace, name string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Exists ¶
Exists reports whether a view template with the given name is registered. Use it to fall back to a default template when a specialized view is absent:
name := "plans." + plan.Slug
if !view.Exists(r, name) {
name = "plans.default"
}
Exists panics if ctx does not carry an application scope.
func Funcs ¶
Funcs returns the template.FuncMap of built-in view helpers bound to ctx. Laravel attaches this map automatically when parsing application templates.
func Render ¶
Render executes the named view template with data and returns the rendered HTML bytes, such as when saving a report or building a custom response. In an HTTP handler, pass r so request-scoped template helpers (such as csrfField, old, and auth) have access to the request. When ctx is not a *web.Request (for example, in a job or mail view), application-scoped helpers such as route and config still work, while request-scoped helpers return an error.
Render returns an error if the template does not exist or fails during execution. It panics if ctx does not carry an application scope.
func Share ¶
Share returns a copy of r carrying value under key so views rendered for that request can read it with the shared template helper, such as displaying the current team in a layout. Call Share in middleware and pass the returned request to next:
return next(view.Share(r, "team", team))
Templates read the value with {{with shared "team"}}{{.Name}}{{end}}. Calling shared with a key that was not shared returns nil.
func VendorAsset ¶
VendorAsset returns the public URL for a file published by a package under namespace, such as a stylesheet, matching the vendorAsset template helper:
link := view.VendorAsset(r, "inspire", "inspire.css")
When the file exists, VendorAsset appends a content hash (?id=...) so browsers bust their cache only when the file changes. If the file does not exist, the URL is returned without a hash. URLs are rooted at the current request's host during a request and at APP_URL otherwise.
VendorAsset panics if ctx does not carry an application scope.
Types ¶
This section is empty.