render

package
v1.0.2 Latest Latest
Warning

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

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

Documentation

Overview

Package render runs the templates of every generator and puts their output together into files.

Index

Constants

View Source
const (
	// Dir is the folder of a set's FS that holds its templates.
	Dir = "templates"
)

Variables

View Source
var (
	ErrTemplate    = errors.New("load templates")
	ErrUnknownPart = errors.New("no template for part")
	ErrExecute     = errors.New("render")
	ErrNoValue     = errors.New("a value that is not set was written as <no value>")
)

Functions

func RenderSource

func RenderSource(src Source, data any) ([]byte, error)

RenderSource parses src on its own, apart from the sets, and runs it on data.

Types

type Engine

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

Engine holds every template of a run. An override is a template apart from the others: one that could run the template that asks for it would never end.

func New

func New(sets []Set, opts Options) (*Engine, error)

New loads the templates of every set, then the block overrides. An override of a block no set declares, or one that does not parse, is a config error.

func (*Engine) RenderFile

func (e *Engine) RenderFile(d FileData) ([]byte, error)

RenderFile puts a file together and formats it when the options ask for that. Each part with text goes on lines of its own below a blank line, so that none runs into the one before it.

func (*Engine) RenderPart

func (e *Engine) RenderPart(part layout.PartID, data any) ([]byte, error)

RenderPart runs the template of part on data.

type FileData

type FileData struct {
	Header  string
	Package string
	Imports string
	Guard   string
	Parts   []string
}

FileData is what a generated file holds around its parts. Header is plain text; Imports is the import declaration; Guard is the runtime constant the file refers to, if any. Parts holds the declarations of each part, with or without line breaks around them.

type Options

type Options struct {
	Templates map[string]string
	Needs     map[string]string
	Format    bool
}

Options are what the engine reads of a config. Templates and Format mirror its keys. Needs says, for each block of a part the config does not write, what the config has to set.

type Set

type Set struct {
	Name   string
	FS     fs.FS
	Parts  map[layout.PartID]string
	Blocks []string
}

Set is the templates of one generator: the .tmpl files in the Dir folder of FS, named Name/<file> once loaded. Parts maps each part to the file that renders it; Blocks lists the blocks a config may override, which a template writes with the override func.

type Source

type Source struct {
	Name  string
	Text  string
	Funcs template.FuncMap
}

Source is a template given as text, such as an extra file's, with the funcs it may call on top of the engine's; one named like a func of the engine replaces it. Name names the template in errors.

Jump to

Keyboard shortcuts

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