Documentation
¶
Overview ¶
Package template provides template rendering and variable substitution.
This package implements a flexible template system that can be used by both prompts and personas. It supports:
- Variable substitution with {{variable}} syntax
- Recursive template resolution (variables can contain other variables)
- Validation of required variables
- Detection of unresolved placeholders
Future versions may support more advanced templating engines like Go templates (similar to Helm charts) for conditional logic, loops, and functions.
Index ¶
- func GetUsedVars(vars map[string]string) []string
- type RenderResult
- type Renderer
- func (r *Renderer) MergeVars(varMaps ...map[string]string) map[string]string
- func (r *Renderer) Render(templateText string, vars map[string]string) (string, error)
- func (r *Renderer) RenderDetailed(templateText string, vars map[string]string) (*RenderResult, error)
- func (r *Renderer) ValidateRequiredVars(requiredVars []string, vars map[string]string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetUsedVars ¶
GetUsedVars returns a list of variable names that had non-empty values. This is useful for debugging and logging which variables were actually used.
Types ¶
type RenderResult ¶ added in v1.3.26
type RenderResult struct {
Text string // final rendered text
UsedVars map[string]string // variables that were substituted
UnusedVars []string // variables available but not referenced
Passes int // number of recursive passes performed
}
RenderResult contains the rendered text and metadata about the rendering process.
type Renderer ¶
type Renderer struct {
}
Renderer handles variable substitution in templates
func (*Renderer) MergeVars ¶
MergeVars merges multiple variable maps with later maps taking precedence. This is useful for combining default values, context variables, and overrides.
Example:
defaults := map[string]string{"color": "blue", "size": "medium"}
overrides := map[string]string{"color": "red"}
result := MergeVars(defaults, overrides)
// result = {"color": "red", "size": "medium"}
func (*Renderer) Render ¶
Render applies variable substitution to the template with recursive resolution.
The renderer performs multiple passes (up to maxPasses) to handle nested variable substitution. For example, if var1="{{var2}}" and var2="value", the final result will correctly resolve to "value".
Returns an error if any placeholders remain unresolved after all passes.
func (*Renderer) RenderDetailed ¶ added in v1.3.26
func (r *Renderer) RenderDetailed(templateText string, vars map[string]string) (*RenderResult, error)
RenderDetailed applies variable substitution and returns detailed rendering metadata.