render

package
v1.7.7 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package render provides template rendering and PDF generation services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheStats

type CacheStats struct {
	Size         int     `json:"size"`          // Number of cached templates
	HitCount     int64   `json:"hit_count"`     // Cache hits
	MissCount    int64   `json:"miss_count"`    // Cache misses
	HitRate      float64 `json:"hit_rate"`      // Hit rate percentage
	MemoryUsage  int64   `json:"memory_usage"`  // Estimated memory usage in bytes
	LastAccessed string  `json:"last_accessed"` // Last cache access time
}

CacheStats represents template cache statistics

type FileInfo

type FileInfo struct {
	Name    string
	Size    int64
	ModTime time.Time
	IsDir   bool
	Path    string
}

FileInfo represents file metadata

type FileReader

type FileReader interface {
	ReadFile(ctx context.Context, path string) ([]byte, error)
	FileExists(ctx context.Context, path string) (bool, error)
	GetFileInfo(ctx context.Context, path string) (FileInfo, error)
}

FileReader defines the interface for reading template files Consumer-driven interface for file system operations

type GoTemplate

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

GoTemplate implements the Template interface using Go's html/template

func (*GoTemplate) Execute

func (t *GoTemplate) Execute(ctx context.Context, data interface{}, writer io.Writer) error

Execute renders the template with the provided data

func (*GoTemplate) ExecuteToString

func (t *GoTemplate) ExecuteToString(ctx context.Context, data interface{}) (string, error)

ExecuteToString renders the template to a string

func (*GoTemplate) GetInfo

func (t *GoTemplate) GetInfo() *TemplateInfo

GetInfo returns template metadata

func (*GoTemplate) Name

func (t *GoTemplate) Name() string

Name returns the template name

func (*GoTemplate) Validate

func (t *GoTemplate) Validate(ctx context.Context) error

Validate checks if the template is valid

type HTMLTemplateEngine

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

HTMLTemplateEngine implements the TemplateEngine interface using Go's html/template

func NewHTMLTemplateEngine

func NewHTMLTemplateEngine(fileReader FileReader, logger Logger) *HTMLTemplateEngine

NewHTMLTemplateEngine creates a new HTML template engine with dependency injection

func (*HTMLTemplateEngine) ClearCache

func (e *HTMLTemplateEngine) ClearCache(ctx context.Context) error

ClearCache clears all cached templates

func (*HTMLTemplateEngine) GetTemplate

func (e *HTMLTemplateEngine) GetTemplate(ctx context.Context, name string) (Template, error)

GetTemplate returns a loaded template by name

func (*HTMLTemplateEngine) LoadTemplate

func (e *HTMLTemplateEngine) LoadTemplate(ctx context.Context, name, path string) error

LoadTemplate loads a template from the specified path

func (*HTMLTemplateEngine) ParseTemplateString

func (e *HTMLTemplateEngine) ParseTemplateString(ctx context.Context, name, content string) error

ParseTemplateString parses a template from a string

func (*HTMLTemplateEngine) ReloadTemplate

func (e *HTMLTemplateEngine) ReloadTemplate(ctx context.Context, name string) error

ReloadTemplate reloads a template from disk

func (*HTMLTemplateEngine) UnloadTemplate

func (e *HTMLTemplateEngine) UnloadTemplate(ctx context.Context, name string) error

UnloadTemplate removes a template from memory

type InvoiceRenderer

type InvoiceRenderer interface {
	// RenderInvoice renders an invoice to HTML using the specified template
	// Returns the rendered HTML content as a string
	RenderInvoice(ctx context.Context, invoice *models.Invoice, templateName string) (string, error)

	// RenderInvoiceToWriter renders an invoice to HTML and writes it to the provided writer
	// Useful for streaming large invoices or writing directly to files
	RenderInvoiceToWriter(ctx context.Context, invoice *models.Invoice, templateName string, writer io.Writer) error

	// ValidateTemplate checks if a template is valid and can be rendered
	// Returns detailed validation errors if template is invalid
	ValidateTemplate(ctx context.Context, templateName string) error

	// ListAvailableTemplates returns the names of all available templates
	ListAvailableTemplates(ctx context.Context) ([]string, error)

	// GetTemplateInfo returns metadata about a specific template
	GetTemplateInfo(ctx context.Context, templateName string) (*TemplateInfo, error)
}

InvoiceRenderer defines the interface for rendering invoices to HTML Consumer-driven interface focusing on invoice generation needs

type Logger

type Logger interface {
	Debug(msg string, keysAndValues ...interface{})
	Info(msg string, keysAndValues ...interface{})
	Warn(msg string, keysAndValues ...interface{})
	Error(msg string, keysAndValues ...interface{})
}

Logger defines the interface for logging operations Consumer-driven interface for logging needs in template rendering

type RenderOptions

type RenderOptions struct {
	TemplateName    string            `json:"template_name"`            // Template to use
	OutputFormat    string            `json:"output_format"`            // "html", "pdf", etc.
	Minify          bool              `json:"minify"`                   // Whether to minify output
	PrettyPrint     bool              `json:"pretty_print"`             // Whether to format output
	Variables       map[string]string `json:"variables,omitempty"`      // Template variables
	IncludeStyles   bool              `json:"include_styles"`           // Whether to include CSS
	Theme           string            `json:"theme,omitempty"`          // Theme name
	Language        string            `json:"language,omitempty"`       // Output language
	CustomCSS       string            `json:"custom_css,omitempty"`     // Custom CSS to include
	WatermarkText   string            `json:"watermark_text,omitempty"` // Watermark text
	ShowLineNumbers bool              `json:"show_line_numbers"`        // Show line numbers in output
}

RenderOptions represents options for rendering operations

type RenderResult

type RenderResult struct {
	Content          string            `json:"content"`                     // Rendered content
	Size             int64             `json:"size"`                        // Content size in bytes
	RenderTimeMs     float64           `json:"render_time_ms"`              // Rendering time in milliseconds
	TemplateName     string            `json:"template_name"`               // Template used
	Variables        map[string]string `json:"variables,omitempty"`         // Variables used
	Warnings         []string          `json:"warnings,omitempty"`          // Non-fatal warnings
	CacheHit         bool              `json:"cache_hit"`                   // Whether template was cached
	CompressionRatio float64           `json:"compression_ratio,omitempty"` // If content was compressed
}

RenderResult represents the result of a rendering operation

type RendererOptions

type RendererOptions struct {
	TemplateDir       string        `json:"template_dir"`
	CacheSize         int           `json:"cache_size"`
	CacheExpiry       time.Duration `json:"cache_expiry"`
	EnableSecurity    bool          `json:"enable_security"`
	EnableCompression bool          `json:"enable_compression"`
	DefaultTemplate   string        `json:"default_template"`
	MaxRenderTime     time.Duration `json:"max_render_time"`
}

RendererOptions represents configuration options for the template renderer

type Template

type Template interface {
	// Execute renders the template with the provided data
	Execute(ctx context.Context, data interface{}, writer io.Writer) error

	// ExecuteToString renders the template to a string
	ExecuteToString(ctx context.Context, data interface{}) (string, error)

	// Name returns the template name
	Name() string

	// GetInfo returns template metadata
	GetInfo() *TemplateInfo

	// Validate checks if the template is valid
	Validate(ctx context.Context) error
}

Template defines the interface for individual templates Provides rendering capabilities for loaded templates

type TemplateCache

type TemplateCache interface {
	// Get retrieves a template from cache
	// Returns ErrCacheMiss if template is not cached
	Get(ctx context.Context, name string) (Template, error)

	// Set stores a template in cache
	Set(ctx context.Context, name string, template Template) error

	// Delete removes a template from cache
	Delete(ctx context.Context, name string) error

	// Clear removes all templates from cache
	Clear(ctx context.Context) error

	// GetStats returns cache statistics
	GetStats(ctx context.Context) (*CacheStats, error)

	// GetSize returns the number of cached templates
	GetSize(ctx context.Context) (int, error)
}

TemplateCache defines the interface for template caching Consumer-driven interface for performance optimization

type TemplateEngine

type TemplateEngine interface {
	// LoadTemplate loads a template from the specified path
	// Returns error if template cannot be loaded or parsed
	LoadTemplate(ctx context.Context, name, path string) error

	// ParseTemplateString parses a template from a string
	// Useful for dynamic template creation or testing
	ParseTemplateString(ctx context.Context, name, content string) error

	// UnloadTemplate removes a template from memory
	UnloadTemplate(ctx context.Context, name string) error

	// ReloadTemplate reloads a template from disk
	// Useful for development and template updates
	ReloadTemplate(ctx context.Context, name string) error

	// GetTemplate returns a loaded template by name
	GetTemplate(ctx context.Context, name string) (Template, error)

	// ClearCache clears all cached templates
	ClearCache(ctx context.Context) error
}

TemplateEngine defines the interface for managing template operations Consumer-driven interface for template system management

type TemplateError

type TemplateError struct {
	Type       string `json:"type"`       // Error type: "syntax", "security", "validation", etc.
	Message    string `json:"message"`    // Human-readable error message
	Template   string `json:"template"`   // Template name where error occurred
	Line       int    `json:"line"`       // Line number (if applicable)
	Column     int    `json:"column"`     // Column number (if applicable)
	Context    string `json:"context"`    // Context around the error
	Suggestion string `json:"suggestion"` // Suggested fix
}

TemplateError represents template-specific errors

func (*TemplateError) Error

func (e *TemplateError) Error() string

Error implements the error interface

func (*TemplateError) IsSecurityError

func (e *TemplateError) IsSecurityError() bool

IsSecurityError returns true if this is a security-related error

func (*TemplateError) IsSyntaxError

func (e *TemplateError) IsSyntaxError() bool

IsSyntaxError returns true if this is a syntax error

type TemplateInfo

type TemplateInfo struct {
	Name        string            `json:"name"`
	Path        string            `json:"path,omitempty"`
	Description string            `json:"description,omitempty"`
	Version     string            `json:"version,omitempty"`
	Author      string            `json:"author,omitempty"`
	CreatedAt   string            `json:"created_at,omitempty"`
	ModifiedAt  string            `json:"modified_at,omitempty"`
	SizeBytes   int64             `json:"size_bytes"`
	Variables   []string          `json:"variables,omitempty"`  // Required template variables
	Includes    []string          `json:"includes,omitempty"`   // Other templates this depends on
	Tags        []string          `json:"tags,omitempty"`       // Template tags/categories
	Metadata    map[string]string `json:"metadata,omitempty"`   // Additional metadata
	IsBuiltIn   bool              `json:"is_built_in"`          // Whether this is a built-in template
	IsValid     bool              `json:"is_valid"`             // Whether template passed validation
	LastError   string            `json:"last_error,omitempty"` // Last validation error
}

TemplateInfo represents metadata about a template

type TemplateRenderer

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

TemplateRenderer implements the InvoiceRenderer interface

func NewTemplateRenderer

func NewTemplateRenderer(engine TemplateEngine, cache TemplateCache, validator TemplateValidator, logger Logger, options *RendererOptions) *TemplateRenderer

NewTemplateRenderer creates a new template renderer with dependency injection

func (*TemplateRenderer) GetTemplateInfo

func (r *TemplateRenderer) GetTemplateInfo(ctx context.Context, templateName string) (*TemplateInfo, error)

GetTemplateInfo returns metadata about a specific template

func (*TemplateRenderer) ListAvailableTemplates

func (r *TemplateRenderer) ListAvailableTemplates(ctx context.Context) ([]string, error)

ListAvailableTemplates returns the names of all available templates

func (*TemplateRenderer) RenderData

func (r *TemplateRenderer) RenderData(ctx context.Context, data interface{}, templateName string) (string, error)

RenderData renders any data to HTML using the specified template (supports data structures)

func (*TemplateRenderer) RenderInvoice

func (r *TemplateRenderer) RenderInvoice(ctx context.Context, invoice *models.Invoice, templateName string) (string, error)

RenderInvoice renders an invoice to HTML using the specified template

func (*TemplateRenderer) RenderInvoiceToWriter

func (r *TemplateRenderer) RenderInvoiceToWriter(ctx context.Context, invoice *models.Invoice, templateName string, writer io.Writer) error

RenderInvoiceToWriter renders an invoice to HTML and writes it to the provided writer

func (*TemplateRenderer) ValidateTemplate

func (r *TemplateRenderer) ValidateTemplate(ctx context.Context, templateName string) error

ValidateTemplate checks if a template is valid and can be rendered

type TemplateValidator

type TemplateValidator interface {
	// ValidateTemplate performs comprehensive template validation
	// Checks for security issues, syntax errors, and best practices
	ValidateTemplate(ctx context.Context, template Template) error

	// ValidateTemplateString validates a template string
	ValidateTemplateString(ctx context.Context, content string) error

	// CheckSecurity performs security-specific validation
	// Checks for potential code injection and unsafe operations
	CheckSecurity(ctx context.Context, template Template) error

	// ValidateData validates that data can be safely used with template
	ValidateData(ctx context.Context, template Template, data interface{}) error
}

TemplateValidator defines the interface for template validation Consumer-driven interface for security and correctness validation

Jump to

Keyboard shortcuts

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