render

package
v1.234.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package render renders Go templates with the Gomplate, Sprig and Atmos function sets. It is the only package that imports gomplate, so the rest of the codebase is insulated from gomplate's library API.

Templates are parsed and executed with text/template directly, lock-free, which matters because stack files are rendered concurrently. Gomplate's renderer-only functions are provided without its renderer: Data reads datasources lazily and the `tmpl` namespace is bound to the parsed template. Gomplate's renderer is used only to read datasources, including the context datasources of Request.ContextSources; it updates package-global metrics, so those reads are serialized.

Index

Constants

View Source
const (
	// MissingKeyError makes execution fail when a map is indexed with a missing key.
	MissingKeyError = "error"
	// MissingKeyDefault substitutes the zero value when a map is indexed with a missing key.
	MissingKeyDefault = "default"
	// DefaultLeftDelim is the default left action delimiter.
	DefaultLeftDelim = "{{"
	// DefaultRightDelim is the default right action delimiter.
	DefaultRightDelim = "}}"
)

Variables

This section is empty.

Functions

func CSV

func CSV(value string) ([][]string, error)

CSV uses Gomplate's native CSV decoder.

func CachedDatasource

func CachedDatasource(sources *Data, alias string, args ...string) (any, error)

CachedDatasource reads a datasource through the process-wide cache that backs `atmos.GomplateDatasource`, so a datasource referenced from many stacks and components is fetched once.

func GomplateFuncs

func GomplateFuncs(ctx context.Context, sources *Data) template.FuncMap

GomplateFuncs returns Gomplate's function map bound to ctx, with the datasource functions served by sources (an empty Data when nil) and, on Windows, the native file namespace. It does not include the `tmpl` namespace, which gomplate only provides through its renderer.

func JSON

func JSON(value string) (map[string]any, error)

JSON uses Gomplate's native JSON and EJSON decoder.

func SprigFuncMap

func SprigFuncMap() template.FuncMap

SprigFuncMap returns the shared Sprig function map. Callers must not mutate it.

func TOML

func TOML(value string) (any, error)

TOML uses Gomplate's native TOML decoder.

func WithExperimental

func WithExperimental(ctx context.Context) context.Context

WithExperimental enables gomplate's experimental functions for renders bound to ctx. Use it instead of gomplate.SetExperimental: it sets both this package's marker (which selects the shared function maps) and Gomplate's own flag. Contexts enabled with gomplate.SetExperimental alone are recognized too, as long as Gomplate's flag can be located (see upstreamExperimentalKey).

Types

type Data

type Data struct {
	Ctx          context.Context
	Sources      map[string]*Source
	ExtraHeaders map[string]http.Header
	// contains filtered or unexported fields
}

Data holds the datasources belonging to a single template evaluation and reads them lazily, the way gomplate's `datasource`/`ds`/`include` functions do. Reads go through gomplate's own datasource reader, captured once per alias from a private gomplate renderer, so datasource values keep gomplate's types (including integers and EJSON values) and the template being rendered never needs gomplate's renderer itself.

Like a gomplate Renderer, a Data value must not be used concurrently.

func NewData

func NewData(sources, headers []string) (*Data, error)

NewData builds datasources from alias=URL and alias=Header: value definitions.

func (*Data) Datasource

func (d *Data) Datasource(alias string, args ...string) (any, error)

Datasource reads and parses a datasource without serializing its native return value.

func (*Data) DatasourceExists

func (d *Data) DatasourceExists(alias string) bool

DatasourceExists reports whether the alias has been registered.

func (*Data) DatasourceReachable

func (d *Data) DatasourceReachable(alias string, args ...string) bool

DatasourceReachable preserves Gomplate's registered-alias probe behavior.

func (*Data) Define

func (d *Data) Define(alias string, source Datasource) error

Define registers a configured datasource definition, parsing its URL as Request.Datasources does (including Windows drive-letter file URLs).

func (*Data) DefineDatasource

func (d *Data) DefineDatasource(alias, value string) (string, error)

DefineDatasource registers an alias once, using Gomplate's own URL parsing rules.

func (*Data) Include

func (d *Data) Include(alias string, args ...string) (string, error)

Include reads a datasource without parsing its contents.

func (*Data) ListDatasources

func (d *Data) ListDatasources() []string

ListDatasources returns the registered aliases in stable order.

type Datasource

type Datasource struct {
	// URL is the datasource URL (any scheme gomplate supports). A bare absolute
	// path is treated as a file:// URL.
	URL string
	// Headers are extra HTTP headers sent to http(s) datasources.
	Headers map[string][]string
}

Datasource is a named gomplate datasource definition.

type DatasourceCache

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

DatasourceCache memoizes datasource reads by alias and arguments so that a datasource referenced many times across a stack is fetched once. It keeps at most a fixed number of entries, evicting the least recently used.

func NewDatasourceCache

func NewDatasourceCache() *DatasourceCache

NewDatasourceCache creates an empty DatasourceCache.

func (*DatasourceCache) Read

func (c *DatasourceCache) Read(sources *Data, alias string, args ...string) (any, error)

Read returns the parsed value of the datasource registered under alias in sources, reading it through sources only on a cache miss. The cache key includes the alias's effective definition, so two stacks that define the same alias differently never see each other's values. Nil values are not cached.

type Engine

type Engine interface {
	Renderer
}

Engine is the Renderer that serves Gomplate, Sprig and caller functions.

func New

func New(opts ...Option) Engine

New creates an Engine with Gomplate and Sprig enabled by default.

type FuncsWrapper

type FuncsWrapper func(funcs template.FuncMap, sources *Data) (template.FuncMap, error)

FuncsWrapper transforms the assembled function map for one render.

type Option

type Option func(*engine)

Option configures an Engine.

func WithDeprecationReporter

func WithDeprecationReporter(reporter compatibility.DeprecationReporter) Option

WithDeprecationReporter overrides the reporter notified when a template uses a gomplate v3 name kept alive by the compatibility shims. The default logs a warning via pkg/logger, deduplicated per template per name.

func WithGomplate

func WithGomplate(enabled bool) Option

WithGomplate enables or disables the Gomplate function set (and datasources).

func WithSprig

func WithSprig(enabled bool) Option

WithSprig enables or disables the Sprig function set.

type Renderer

type Renderer interface {
	Render(ctx context.Context, req *Request) (string, error)
}

Renderer renders templates.

type Request

type Request struct {
	// Name is the template name used in error messages.
	Name string
	// Text is the template source.
	Text string
	// Data is the value bound to `.` when the template executes. It may be a
	// map or any Go value; it is never serialized.
	Data any
	// Funcs are functions merged last, so they override Gomplate and Sprig
	// functions with the same name (e.g. the `atmos` namespace).
	Funcs template.FuncMap
	// LeftDelim and RightDelim override the default action delimiters.
	LeftDelim  string
	RightDelim string
	// MissingKey is the text/template "missingkey" option; empty means MissingKeyError.
	MissingKey string
	// Datasources are read lazily when the template calls `datasource`/`ds`/`include`.
	// They are added to Sources (or to a fresh Data when Sources is nil).
	Datasources map[string]Datasource
	// Sources serves the template's datasource functions. Pass the same Data to
	// functions that read datasources themselves (such as the `atmos`
	// namespace) so they share its definitions. Nil means a fresh Data.
	Sources *Data
	// ContextSources are read eagerly before execution and exposed on `.`; the
	// alias "." replaces the whole context, as in gomplate's renderer. When set,
	// Data is ignored. They are also readable as datasources (for example
	// `ds "config"`).
	ContextSources map[string]Datasource
	// WrapFuncs, when set, receives the fully assembled function map (Gomplate,
	// Sprig, compatibility shims and Funcs) and the render's datasources, and
	// returns the map the template is parsed with. Policy admission uses it to
	// guard every function call. Gomplate's `tmpl` namespace and `tpl` alias
	// need the parsed root, so they are passed through it in a second call of
	// their own once they are bound.
	WrapFuncs FuncsWrapper
	// Prepare, when set, runs on the parsed template before it executes, for
	// callers that rewrite or guard the parse tree. Gomplate's `tmpl`/`tpl`
	// functions are unavailable when it is set, so run-time templates can't
	// escape it. It does not apply to ContextSources renders.
	Prepare func(*template.Template)
}

Request describes one template render.

type Source

type Source struct {
	Alias  string
	URL    *url.URL
	Header http.Header
}

Source retains the URL and headers so policy admission can inspect a datasource before reading it.

Jump to

Keyboard shortcuts

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