Documentation
¶
Overview ¶
Package templator provides a type-safe template rendering system for Go applications. It offers a simple and concurrent-safe way to manage HTML templates with compile-time type checking for template data.
Example ¶
package main import ( "bytes" "context" "fmt" "testing/fstest" "github.com/alesr/templator" ) func main() { // Create template files in memory fs := fstest.MapFS{ "templates/home.html": &fstest.MapFile{ Data: []byte(`<h1>{{.Title}}</h1><p>{{.Content}}</p>`), }, "templates/team.html": &fstest.MapFile{ Data: []byte(`<h2>{{.Title}}</h2><p>{{.Content}}</p>`), }, } // Create a new registry reg, _ := templator.NewRegistry[templator.TestData](fs) // Get and execute home template home, _ := reg.Get("home") var homeOutput bytes.Buffer home.Execute(context.Background(), &homeOutput, templator.TestData{ Title: "Welcome", Content: "Hello, World!", }) // Get and execute team template team, _ := reg.Get("team") var teamOutput bytes.Buffer team.Execute(context.Background(), &teamOutput, templator.TestData{ Title: "Engineering", Content: "Building amazing things", }) // Print the outputs fmt.Printf("Home template output:\n%s\n\n", homeOutput.String()) fmt.Printf("Team template output:\n%s\n", teamOutput.String()) }
Output: Home template output: <h1>Welcome</h1><p>Hello, World!</p> Team template output: <h2>Engineering</h2><p>Building amazing things</p>
Index ¶
Examples ¶
Constants ¶
const ( // DefaultTemplateDir is the default directory where templates are stored. DefaultTemplateDir = "templates" // DefaultTemplateExt is the default file extension for templates. DefaultTemplateExt = "html" // ExtensionHTML defines the standard HTML template file extension. ExtensionHTML Extension = ".html" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrTemplateExecution ¶
ErrTemplateExecution is returned when a template fails to execute.
func (ErrTemplateExecution) Error ¶
func (e ErrTemplateExecution) Error() string
func (ErrTemplateExecution) Unwrap ¶
func (e ErrTemplateExecution) Unwrap() error
type ErrTemplateNotFound ¶
type ErrTemplateNotFound struct {
Name string
}
ErrTemplateNotFound is returned when a template cannot be found.
func (ErrTemplateNotFound) Error ¶
func (e ErrTemplateNotFound) Error() string
type Handler ¶
type Handler[T any] struct { // contains filtered or unexported fields }
Handler manages a specific template instance with type-safe data handling. It provides methods for template execution and customization.
type Option ¶
Option configures a Registry instance.
func WithFieldValidation ¶
WithFieldValidation enables template field validation against the provided model
func WithTemplatesPath ¶
WithTemplatesPath returns an Option that sets a custom template directory path. If an empty path is provided, the default path will be used.
type Registry ¶
type Registry[T any] struct { // contains filtered or unexported fields }
Registry manages template handlers in a concurrent-safe manner.
func NewRegistry ¶
NewRegistry creates a new template registry with the provided filesystem and options. It accepts a filesystem interface and variadic options for customization.
type ValidationError ¶
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string