template

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: BSD-3-Clause Imports: 20 Imported by: 0

README

template

Jinja2-compatible templating with Ansible's filter and test library, pure Go CGO=0.

Part of go-ansible — a pure-Go (CGO=0), functional-parity port of Ansible.

CI Go Reference License

Documentation

Overview

Package template implements Ansible's templating semantics on top of a Jinja2-compatible engine (github.com/nikolalohinski/gonja/v2): string interpolation with {{ }}, control structures with {% %}, Ansible's own filter and test library on top of the Jinja2 built-ins, and Ansible's rule that a value which is a SINGLE {{ expr }} with nothing else around it renders to the expression's native type (list, dict, int, bool...), not a string.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsTemplate

func IsTemplate(s string) bool

IsTemplate reports whether s contains any Jinja2 delimiter ({{, {%, or {#) and therefore needs rendering at all.

Types

type Engine

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

Engine renders Ansible-flavored Jinja2 templates and expressions.

func New

func New() *Engine

New returns an Engine with Jinja2's built-in filters/tests plus Ansible's filter and test library.

func (*Engine) Eval

func (e *Engine) Eval(exprSrc string, data map[string]any) (any, error)

Eval evaluates a single Jinja2 expression (no {{ }} wrapper — e.g. the raw text of an Ansible `when:` condition, or the inside of a wholeExpression string) and returns its native Go value.

func (*Engine) EvalBool

func (e *Engine) EvalBool(exprSrc string, data map[string]any) (bool, error)

EvalBool evaluates exprSrc and applies Ansible/Jinja2 truthiness — the semantics of a `when:` condition.

func (*Engine) Render

func (e *Engine) Render(src string, data map[string]any) (string, error)

Render renders a full template string (text mixed with {{ }} / {% %}) to its string form.

func (*Engine) RenderValue

func (e *Engine) RenderValue(raw any, data map[string]any) (any, error)

RenderValue applies Ansible's templating rule to an arbitrary decoded YAML value: strings are templated (with the whole-expression rule preserving native types), and lists/maps are walked recursively so a nested `{{ }}` anywhere in a task's arguments is resolved. Non-string scalars pass through unchanged.

Jump to

Keyboard shortcuts

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