package renderer

import "github.com/go-arrower/arrower/renderer"

Package renderer provides renderers for web views and unit tests.

The renderer expects a fs.FS with the following folder structure:

Index

Constants

const (
	SharedViews = ""
)

Variables

var (
	ErrCreateRendererFailed = errors.New("create renderer failed")

	ErrRenderFailed       = errors.New("rendering failed")
	ErrNotExistsComponent = fmt.Errorf("%w: component does not exist", ErrRenderFailed)
	ErrNotExistsPage      = fmt.Errorf("%w: page does not exist", ErrRenderFailed)
	ErrNotExistsFragment  = fmt.Errorf("%w: fragment does not exist", ErrRenderFailed)
	ErrNotExistsLayout    = fmt.Errorf("%w: layout does not exist", ErrRenderFailed) // todo new err for base as well?
	ErrContextNotAdded    = errors.New("context not added")
)

Types

type DataFunc

type DataFunc func(ctx context.Context) (map[string]any, error)

type EchoRenderer

type EchoRenderer struct {
	*Renderer
}

EchoRenderer is a wrapper that makes the Renderer available for the echo router: https://echo.labstack.com/

func NewEchoRenderer

func NewEchoRenderer(
	logger alog.Logger,
	traceProvider trace.TracerProvider,
	echo *echo.Echo,
	viewFS fs.FS,
	hotReload bool,
) (*EchoRenderer, error)

func (*EchoRenderer) Render

func (r *EchoRenderer) Render(w io.Writer, templateName string, data interface{}, c echo.Context) error

type Map

type Map map[string]any

type Renderer

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

func New

func New(
	logger alog.Logger,
	traceProvider trace.TracerProvider,
	viewFS fs.FS,
	funcMap template.FuncMap,
	hotReload bool,
) (*Renderer, error)

New prepares a renderer for HTML web views.

func (*Renderer) AddBaseData

func (r *Renderer) AddBaseData(baseName string, dataFunc DataFunc) error

func (*Renderer) AddContext

func (r *Renderer) AddContext(name string, viewFS fs.FS) error

func (*Renderer) AddLayoutData

func (r *Renderer) AddLayoutData(context string, layoutName string, dataFunc DataFunc) error

func (*Renderer) Render

func (r *Renderer) Render(ctx context.Context, w io.Writer, contextName string, templateName string, data interface{}) error

type RendererAssertions

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

RendererAssertions is a helper that exposes a lot of TestRenderer-specific assertions for the use in tests. The interface follows stretchr/testify as close as possible.

func (*RendererAssertions) Contains

func (a *RendererAssertions) Contains(contains any, msgAndArgs ...any) bool

func (*RendererAssertions) NotEmpty

func (a *RendererAssertions) NotEmpty(msgAndArgs ...any) bool

NotEmpty asserts that the html is not empty.

type TestRenderer

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

func Test

func Test(
	viewFS fs.FS,
	funcMap template.FuncMap,
) (*TestRenderer, error)

func (*TestRenderer) Render

func (r *TestRenderer) Render(
	t *testing.T,
	ctx context.Context,
	context string,
	name string,
	data interface{},
) (*RendererAssertions, error)

Source Files

echo-renderer.go renderer.go testing.go

Version
v0.0.0-20250311203644-ab26c1152cb4 (latest)
Published
Mar 11, 2025
Platform
linux/amd64
Imports
21 packages
Last checked
1 week ago

Tools for package owners.