view

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 30 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exists

func Exists(ctx context.Context, name string) bool

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

func Funcs(ctx context.Context) template.FuncMap

Funcs returns the template.FuncMap of built-in view helpers bound to ctx. Laravel attaches this map automatically when parsing application templates.

func Render

func Render(ctx context.Context, name string, data any) ([]byte, error)

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

func Share(r *web.Request, key string, value any) *web.Request

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

func VendorAsset(ctx context.Context, namespace, name string) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL