Documentation
¶
Overview ¶
Example (CustomImportResolver) ¶
package main
import (
"log"
echotemplates "github.com/mkozhukh/echo-templates"
)
func main() {
// Create a custom source with import resolution
type customSource struct {
echotemplates.TemplateSource
baseDir string
}
// Implement custom import resolution
type customFileSource struct {
*echotemplates.FileSystemSource
}
baseSource, _ := echotemplates.NewFileSystemSource("./templates")
// Wrap the source to override import resolution
source := &struct {
echotemplates.TemplateSource
}{
TemplateSource: baseSource,
}
// Note: In a real implementation, you would create a proper type
// that embeds FileSystemSource and overrides the ResolveImport method
engine, err := echotemplates.New(echotemplates.Config{
Source: source,
})
if err != nil {
log.Fatal(err)
}
// Use the engine...
_ = engine
}
Output:
Example (FileWatching) ¶
package main
import (
"fmt"
"log"
"os"
"path/filepath"
echotemplates "github.com/mkozhukh/echo-templates"
)
func main() {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "templates")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Create a filesystem source
source, err := echotemplates.NewFileSystemSource(tmpDir)
if err != nil {
log.Fatal(err)
}
// Create engine with dev mode (enables file watching)
engine, err := echotemplates.New(echotemplates.Config{
Source: source,
DevMode: true,
})
if err != nil {
log.Fatal(err)
}
// Create a template file
templatePath := filepath.Join(tmpDir, "test.md")
err = os.WriteFile(templatePath, []byte("Hello {{name}}!"), 0644)
if err != nil {
log.Fatal(err)
}
// Generate with initial content
messages, _ := engine.Generate("test", map[string]any{"name": "World"})
fmt.Printf("Initial: %s\n", messages[0].Content)
// Modify the template file
err = os.WriteFile(templatePath, []byte("Hi {{name}}!"), 0644)
if err != nil {
log.Fatal(err)
}
// Wait a moment for file watcher to detect change
// (In real usage, this happens automatically)
// Generate again - will use updated content
messages, _ = engine.Generate("test", map[string]any{"name": "World"})
fmt.Printf("Updated: %s\n", messages[0].Content)
}
Output:
Example (StringGeneration) ¶
package main
import (
"fmt"
echotemplates "github.com/mkozhukh/echo-templates"
)
func main() {
// Generate messages directly from a string template
messages, _ := echotemplates.Generate("Hello {{name}}!", map[string]any{"name": "World"})
fmt.Printf("Content: %s\n", messages[0].Content)
}
Output: Content: Hello World!
Index ¶
- func CallOptions(metadata map[string]any) []echo.CallOption
- func Extend(metadata map[string]any, content string) map[string]any
- func Generate(content string, vars map[string]any, opts ...GenerateOptions) ([]echo.Message, error)
- func GenerateWithMetadata(content string, vars map[string]any, opts ...GenerateOptions) ([]echo.Message, map[string]any, error)
- type Config
- type EmbedSource
- func (s *EmbedSource) List() ([]string, error)
- func (s *EmbedSource) Open(path string) (io.ReadCloser, error)
- func (s *EmbedSource) ResolveImport(importPath, currentPath string) string
- func (s *EmbedSource) Stat(path string) (TemplateInfo, error)
- func (s *EmbedSource) StopWatch() error
- func (s *EmbedSource) Watch() (<-chan string, error)
- type FileSystemSource
- func (s *FileSystemSource) List() ([]string, error)
- func (s *FileSystemSource) Open(path string) (io.ReadCloser, error)
- func (s *FileSystemSource) ResolveImport(importPath, currentPath string) string
- func (s *FileSystemSource) Stat(path string) (TemplateInfo, error)
- func (s *FileSystemSource) StopWatch() error
- func (s *FileSystemSource) Watch() (<-chan string, error)
- type GenerateOptions
- type ImportError
- type MockSource
- func (m *MockSource) List() ([]string, error)
- func (m *MockSource) Open(path string) (io.ReadCloser, error)
- func (m *MockSource) ResolveImport(importPath, currentPath string) string
- func (m *MockSource) Stat(path string) (TemplateInfo, error)
- func (m *MockSource) StopWatch() error
- func (m *MockSource) Watch() (<-chan string, error)
- type ParseError
- type TemplateEngine
- type TemplateInfo
- type TemplateNotFoundError
- type TemplateSource
- type VariableError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallOptions ¶ added in v0.2.0
func CallOptions(metadata map[string]any) []echo.CallOption
CallOptions creates echo.CallOption slice from template metadata
Types ¶
type Config ¶
type Config struct {
// Source is the template source (required)
Source TemplateSource
// DevMode disables caching for development (default: false)
DevMode bool
// DefaultOptions applies to all Generate calls unless overridden
DefaultOptions GenerateOptions
// CacheSize maximum number of templates to cache in production mode (default: 100)
CacheSize int
}
Config configures the template engine
type EmbedSource ¶
type EmbedSource struct {
// contains filtered or unexported fields
}
EmbedSource implements TemplateSource for embedded templates
Example ¶
package main
import (
"embed"
"fmt"
"log"
echotemplates "github.com/mkozhukh/echo-templates"
)
func main() {
// Example with embedded templates
// Assume you have:
// //go:embed prompts/*
// var embeddedTemplates embed.FS
// For this example, we'll create a dummy embed.FS
var embeddedTemplates embed.FS
// Create an embedded source
source := echotemplates.NewEmbedSource(embeddedTemplates, "prompts")
// Create engine in production mode (with caching)
engine, err := echotemplates.New(echotemplates.Config{
Source: source,
DevMode: false,
CacheSize: 100,
})
if err != nil {
log.Fatal(err)
}
// Generate messages
messages, err := engine.Generate("example", map[string]any{
"topic": "AI",
})
if err != nil {
log.Fatal(err)
}
for _, msg := range messages {
fmt.Printf("Role: %s, Content: %s\n", msg.Role, msg.Content)
}
}
Output:
func NewEmbedSource ¶
func NewEmbedSource(embedFS embed.FS, rootDir string) *EmbedSource
NewEmbedSource creates a new embedded template source
func (*EmbedSource) List ¶
func (s *EmbedSource) List() ([]string, error)
List returns all available template paths
func (*EmbedSource) Open ¶
func (s *EmbedSource) Open(path string) (io.ReadCloser, error)
Open returns a reader for the template content
func (*EmbedSource) ResolveImport ¶
func (s *EmbedSource) ResolveImport(importPath, currentPath string) string
ResolveImport allows customizing import resolution
func (*EmbedSource) Stat ¶
func (s *EmbedSource) Stat(path string) (TemplateInfo, error)
Stat returns information about a template
func (*EmbedSource) StopWatch ¶
func (s *EmbedSource) StopWatch() error
StopWatch is a no-op for embedded templates
func (*EmbedSource) Watch ¶
func (s *EmbedSource) Watch() (<-chan string, error)
Watch returns nil as embedded templates don't change
type FileSystemSource ¶
type FileSystemSource struct {
// contains filtered or unexported fields
}
FileSystemSource implements TemplateSource for filesystem-based templates
Example ¶
package main
import (
"fmt"
"log"
echotemplates "github.com/mkozhukh/echo-templates"
)
func main() {
// Create a filesystem source
source, err := echotemplates.NewFileSystemSource("./templates")
if err != nil {
log.Fatal(err)
}
// Create engine with dev mode enabled (no caching, file watching)
engine, err := echotemplates.New(echotemplates.Config{
Source: source,
DevMode: true,
})
if err != nil {
log.Fatal(err)
}
// Generate messages
messages, err := engine.Generate("hello", map[string]any{
"name": "World",
})
if err != nil {
log.Fatal(err)
}
for _, msg := range messages {
fmt.Printf("Role: %s, Content: %s\n", msg.Role, msg.Content)
}
}
Output:
func NewFileSystemSource ¶
func NewFileSystemSource(rootDir string) (*FileSystemSource, error)
NewFileSystemSource creates a new filesystem template source
func (*FileSystemSource) List ¶
func (s *FileSystemSource) List() ([]string, error)
List returns all available template paths
func (*FileSystemSource) Open ¶
func (s *FileSystemSource) Open(path string) (io.ReadCloser, error)
Open returns a reader for the template content
func (*FileSystemSource) ResolveImport ¶
func (s *FileSystemSource) ResolveImport(importPath, currentPath string) string
ResolveImport allows customizing import resolution
func (*FileSystemSource) Stat ¶
func (s *FileSystemSource) Stat(path string) (TemplateInfo, error)
Stat returns information about a template
func (*FileSystemSource) StopWatch ¶
func (s *FileSystemSource) StopWatch() error
StopWatch stops watching for changes
func (*FileSystemSource) Watch ¶
func (s *FileSystemSource) Watch() (<-chan string, error)
Watch starts watching for changes
type GenerateOptions ¶
type GenerateOptions struct {
// AllowMissingVars determines if missing placeholders cause errors
AllowMissingVars bool
// StrictMode enables strict parsing (no undefined imports, etc)
StrictMode bool
// DisableCache bypasses cache for this generation
DisableCache bool
}
GenerateOptions configures template generation behavior
type ImportError ¶
ImportError indicates a failure during template import
func (*ImportError) Error ¶
func (e *ImportError) Error() string
type MockSource ¶ added in v0.2.0
type MockSource struct {
// contains filtered or unexported fields
}
MockSource implements TemplateSource for testing purposes using an in-memory map
func NewMockSource ¶ added in v0.2.0
func NewMockSource(templates map[string]string) *MockSource
NewMockSource creates a new mock template source with the given templates
func (*MockSource) List ¶ added in v0.2.0
func (m *MockSource) List() ([]string, error)
List returns all available template paths
func (*MockSource) Open ¶ added in v0.2.0
func (m *MockSource) Open(path string) (io.ReadCloser, error)
Open returns a reader for the template content
func (*MockSource) ResolveImport ¶ added in v0.2.0
func (m *MockSource) ResolveImport(importPath, currentPath string) string
ResolveImport returns empty string - no custom import resolution
func (*MockSource) Stat ¶ added in v0.2.0
func (m *MockSource) Stat(path string) (TemplateInfo, error)
Stat returns information about a template
func (*MockSource) StopWatch ¶ added in v0.2.0
func (m *MockSource) StopWatch() error
StopWatch is a no-op for mock
func (*MockSource) Watch ¶ added in v0.2.0
func (m *MockSource) Watch() (<-chan string, error)
Watch returns nil channel - watching not supported for mock
type ParseError ¶
ParseError indicates a template parsing error
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type TemplateEngine ¶
type TemplateEngine interface {
// Generate creates messages from a template
// If name doesn't contain .md suffix, it will be added automatically
Generate(name string, vars map[string]any, opts ...GenerateOptions) ([]echo.Message, error)
// GenerateWithMetadata creates messages and returns template metadata
GenerateWithMetadata(name string, vars map[string]any, opts ...GenerateOptions) ([]echo.Message, map[string]any, error)
// ClearCache removes cached templates (useful for development)
ClearCache()
// ValidateTemplate checks if a template is valid without generating messages
ValidateTemplate(name string) error
// GetTemplateVariables returns all variable names used in a template
GetTemplateVariables(name string) ([]string, error)
// TemplateExists checks if a template file exists
TemplateExists(name string) bool
// ListTemplates returns all available template paths relative to RootDir
ListTemplates() ([]string, error)
}
TemplateEngine manages template loading and processing
type TemplateInfo ¶
type TemplateInfo struct {
// Path is the template path
Path string
// ModTime is the modification time
ModTime time.Time
// Size is the template size in bytes
Size int64
// IsDir indicates if this is a directory
IsDir bool
}
TemplateInfo contains information about a template
type TemplateNotFoundError ¶
TemplateNotFoundError indicates that a template file was not found
func (*TemplateNotFoundError) Error ¶
func (e *TemplateNotFoundError) Error() string
type TemplateSource ¶
type TemplateSource interface {
// Open returns a reader for the template content
Open(path string) (io.ReadCloser, error)
// Stat returns information about a template
Stat(path string) (TemplateInfo, error)
// List returns all available template paths
List() ([]string, error)
// Watch starts watching for changes if supported
// The returned channel will receive paths of changed templates
// Returns nil if watching is not supported
Watch() (<-chan string, error)
// StopWatch stops watching for changes
StopWatch() error
// ResolveImport allows customizing import resolution
// Given an import path and the current template path, returns the resolved path
// Return empty string to use default resolution
ResolveImport(importPath, currentPath string) string
}
TemplateSource abstracts the source of templates (filesystem, embedded, etc.)
type VariableError ¶
VariableError indicates a missing or invalid variable
func (*VariableError) Error ¶
func (e *VariableError) Error() string