view

package
v0.0.2-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package view renders Go html/template files for hex web apps.

The Engine loads *.gotmpl templates from an fs.FS (typically an //go:embed of web/views/) at construction and exposes:

  • Render — full-page rendering, satisfies echo.Renderer so handlers can call c.Render(200, "pages/home", data).
  • Partial — renders a named {{ define }} block only, suited to HTMX responses that swap fragments instead of full pages.

Templates use standard html/template syntax, so layouts and partials compose via {{ template "name" . }}. hex ships no additional syntax or helpers — consumers add funcs via WithFuncs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

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

Engine holds a parsed template set + optional per-render helpers.

func New

func New(fsys fs.FS, opts ...Option) (*Engine, error)

New parses every template file under the given FS matching the configured extension. Templates are named by their path relative to the scan root, minus the extension. So a file at `web/views/pages/home.gotmpl` (with fsys rooted at `web/views/`) registers as `pages/home`.

func (*Engine) Lookup

func (e *Engine) Lookup(name string) *template.Template

Lookup returns the underlying *template.Template for name. Nil when not found. Exposed for consumers who need to hold onto a specific template (e.g. for tests or lower-level rendering).

func (*Engine) Names

func (e *Engine) Names() string

Names returns the sorted list of registered template names for error messages and debugging.

func (*Engine) Partial

func (e *Engine) Partial(w io.Writer, block string, data any) error

Partial renders a named {{ define }} block, suitable for HTMX responses that return an HTML fragment. Unlike Render, Partial does not require the block to be a top-level template file — any {{ define "block-name" }} inside the loaded set works.

func (*Engine) Render

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

Render implements echo.Renderer. Consumers call:

return c.Render(http.StatusOK, "pages/home", data)

The template name is the file path relative to the view root, without extension.

type Option

type Option func(*config)

Option configures a new Engine.

func WithDir

func WithDir(dir string) Option

WithDir sets the subdirectory within the FS to scan. Defaults to the root of the FS.

func WithExtension

func WithExtension(ext string) Option

WithExtension registers a template file extension. Files ending with ext are loaded and parsed as plain html/template source. Call multiple times to accept several extensions; call WithPreprocessor instead for extensions that need translation (Jade/Pug, Markdown, ...).

The default engine accepts ".gotmpl" only.

func WithFuncs

func WithFuncs(funcs template.FuncMap) Option

WithFuncs registers template funcs available in every template.

func WithPreprocessor

func WithPreprocessor(ext string, fn Preprocessor) Option

WithPreprocessor registers a Preprocessor for files matching ext. The preprocessor's output is fed to html/template. Useful for:

  • Jade/Pug (github.com/Joker/jade) via hex/view/jade
  • Markdown via any markdown-to-html transformer
  • Mustache-like DSLs that need translation

Multiple extensions with different preprocessors can coexist — call WithPreprocessor once per extension.

type Preprocessor

type Preprocessor func(name string, source []byte) (string, error)

Preprocessor converts arbitrary template source (e.g. Jade/Pug, Markdown) to html/template-compatible source before the Engine parses it. name is the template's path relative to the view root (useful for error messages); source is the raw file contents.

Return the html/template string plus any error. A nil error with an empty string is treated as "skip this template".

Directories

Path Synopsis
Package jade wires github.com/Joker/jade into hex/view as a template Preprocessor.
Package jade wires github.com/Joker/jade into hex/view as a template Preprocessor.
Package md wires github.com/yuin/goldmark into hex/view as a Markdown → HTML Preprocessor.
Package md wires github.com/yuin/goldmark into hex/view as a Markdown → HTML Preprocessor.
Package provider installs a hex/view Engine as the *web.Server's echo.Renderer and binds the engine under "view" in the container.
Package provider installs a hex/view Engine as the *web.Server's echo.Renderer and binds the engine under "view" in the container.

Jump to

Keyboard shortcuts

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