-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
semihalev edited this page Mar 11, 2025
·
1 revision
go get github.com/semihalev/twig
package main
import (
"fmt"
"github.com/semihalev/twig"
"os"
)
func main() {
// Create a new Twig engine
engine := twig.New()
// Add a template loader
loader := twig.NewFileSystemLoader([]string{"./templates"})
engine.RegisterLoader(loader)
// Render a template
context := map[string]interface{}{
"name": "World",
"items": []string{"apple", "banana", "orange"},
}
// Render to a string
result, err := engine.Render("index.twig", context)
if err \!= nil {
fmt.Println("Error:", err)
return
}
fmt.Println(result)
// Or render directly to a writer
err = engine.RenderTo(os.Stdout, "index.twig", context)
if err \!= nil {
fmt.Println("Error:", err)
return
}
}
- Go 1.18 or higher
- No external dependencies required (all dependencies are included in Go's standard library)
To run the test suite:
go test ./...
For tests with coverage report:
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
- Review Template Syntax to learn about Twig's powerful templating capabilities
- Explore Filters and Functions to extend your templates
- Learn about Macros for creating reusable template components