Help us shape the future of Go! Take the 2026 Go Developer Survey

view

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package view renders html/template views with layouts, partials and fragments (single blocks, for htmx), plus request-aware helpers: route, asset, csrfField, old, errors, flash and cspNonce.

Layout of the views directory:

layouts/app.html        {{block "content" .}}{{end}} inside the page shell
partials/nav.html       included with {{template "partials/nav" .}}
components/alert.html   same, by convention for reusable widgets
posts/index.html        a page: {{define "content"}}...{{end}}

A page uses the default layout ("app") unless its first line is a comment naming another one: {{/* layout: admin */}} or {{/* layout: none */}}.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// FS holds the views (os.DirFS("resources/views") or an embed.FS).
	FS fs.FS
	// Ext is the view file extension (default ".html").
	Ext string
	// DefaultLayout is used by pages without a layout comment (default
	// "app"; ignored when layouts/app.html does not exist).
	DefaultLayout string
	// Reload re-parses views on every render (development).
	Reload bool
	// Funcs adds custom template functions.
	Funcs template.FuncMap
	// URLs resolves named routes for the "route" function.
	URLs web.URLGenerator
	// AssetPrefix is prepended by "asset" (default "/").
	AssetPrefix string
}

Config configures an Engine.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine renders views; it implements web.Renderer.

func New

func New(cfg Config) (*Engine, error)

New parses every view, so syntax errors surface at boot.

func (*Engine) Has

func (e *Engine) Has(name string) bool

Has implements web.Renderer.

func (*Engine) Names

func (e *Engine) Names() []string

Names lists the page names.

func (*Engine) Render

func (e *Engine) Render(ctx context.Context, w io.Writer, name string, data any) error

Render implements web.Renderer: it renders the page (inside its layout).

func (*Engine) RenderBlock

func (e *Engine) RenderBlock(ctx context.Context, w io.Writer, name, block string, data any) error

RenderBlock implements web.Renderer: it renders a single {{block}} or {{define}} of the page, without the layout.

Jump to

Keyboard shortcuts

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