template

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package template provides prompt template rendering with variable substitution.

The template engine supports both Go template syntax and a simplified Handlebars-like syntax that is automatically converted before execution.

Syntax

Simple variables use double braces:

Hello, {{name}}!

Conditionals use #if and /if:

{{#if urgent}}URGENT: {{/if}}{{title}}

Iteration uses #each and /each:

{{#each items}}{{.}} {{/each}}

Helper functions can be called with arguments:

{{truncate description 100}}
{{upper name}}

Built-in Functions

  • truncate(s string, maxLen int) string - Cut string to max length with ellipsis
  • json(v any) string - Convert value to pretty-printed JSON
  • upper(s string) string - Convert to uppercase
  • lower(s string) string - Convert to lowercase
  • trim(s string) string - Remove leading/trailing whitespace
  • split(s, sep string) []string - Split string by separator
  • join(slice []string, sep string) string - Join strings with separator
  • replace(s, old, new string) string - Replace all occurrences
  • contains(s, substr string) bool - Check if string contains substring
  • hasPrefix(s, prefix string) bool - Check if string starts with prefix
  • hasSuffix(s, suffix string) bool - Check if string ends with suffix
  • default(val, defaultVal any) any - Return default if val is nil/empty
  • indent(s string, spaces int) string - Add spaces to each line
  • wrap(s string, width int) string - Wrap text at width

Example

engine := template.NewEngine()
result, err := engine.Render("Hello, {{name}}!", map[string]any{"name": "World"})
// result: "Hello, World!"

Variable Extraction

The Parse method extracts variable names from a template:

vars, err := engine.Parse("{{greeting}}, {{name}}!")
// vars: ["greeting", "name"]

Custom Functions

Add custom functions using AddFunc:

engine.AddFunc("double", func(s string) string { return s + s })
result, _ := engine.Render("{{double .name}}", map[string]any{"name": "ha"})
// result: "haha"

Note: Custom functions use Go template syntax (.name) not Handlebars syntax.

Location

This package is part of the llmkit library:

import "github.com/randalmurphal/llmkit/template"

Package template provides prompt template rendering with variable substitution.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmpty is returned when the template string is empty.
	ErrEmpty = errors.New("template is empty")

	// ErrParse is returned when the template fails to parse.
	ErrParse = errors.New("template parse error")

	// ErrExecute is returned when template execution fails.
	ErrExecute = errors.New("template execution error")

	// ErrVariable is returned when a required variable is missing.
	ErrVariable = errors.New("required variable missing")
)

Sentinel errors for template operations.

Functions

func ValidateVariables

func ValidateVariables(required []string, provided map[string]any) error

ValidateVariables checks that all required variables are provided. Returns an error wrapping ErrVariable if any required variable is missing.

Types

type Engine

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

Engine renders prompt templates with variable substitution. It supports both Go template syntax and Handlebars-like syntax.

func NewEngine

func NewEngine() *Engine

NewEngine creates a new template engine with default helper functions.

func (*Engine) AddFunc

func (e *Engine) AddFunc(name string, fn any)

AddFunc adds a custom template function. The function will be available in templates using the given name.

func (*Engine) Parse

func (e *Engine) Parse(templateStr string) ([]string, error)

Parse validates the template and extracts variable names. Returns a list of variable names referenced in the template.

func (*Engine) Render

func (e *Engine) Render(templateStr string, variables map[string]any) (string, error)

Render executes the template with the given variables. The template string supports Handlebars-like syntax which is automatically converted to Go template syntax before execution.

Jump to

Keyboard shortcuts

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