Twig is a fast, memory-efficient Twig template engine implementation for Go.
Find a file
semihalev 512a48bb41 Implement macro functionality
- Enhanced lexer to recognize macro-specific tokens
- Added advanced token detection for expressions
- Implemented parseMacro, parseImport, and parseFrom methods
- Added lexical support for keywords like 'macro', 'import', 'from', 'as'
- Created test cases for macro definition and usage
- Added support for importing macros between templates
- Improved lexer with advanced token scanning for proper macro handling

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-10 05:03:29 +03:00
examples Fix lexer generator template issues 2025-03-10 04:56:55 +03:00
gen Implement macro functionality 2025-03-10 05:03:29 +03:00
tools Implement macro functionality 2025-03-10 05:03:29 +03:00
expr.go Initial Twig template engine implementation 2025-03-10 04:11:43 +03:00
extension.go Initial Twig template engine implementation 2025-03-10 04:11:43 +03:00
go.mod Initial Twig template engine implementation 2025-03-10 04:11:43 +03:00
LICENSE Initial Twig template engine implementation 2025-03-10 04:11:43 +03:00
loader.go Initial Twig template engine implementation 2025-03-10 04:11:43 +03:00
macro_test.go Implement macro functionality 2025-03-10 05:03:29 +03:00
node.go Fix lexer generator template issues 2025-03-10 04:56:55 +03:00
parser.go Fix lexer generator template issues 2025-03-10 04:56:55 +03:00
README.md Initial Twig template engine implementation 2025-03-10 04:11:43 +03:00
render.go Add control structures and template inheritance support 2025-03-10 04:24:51 +03:00
twig.go Fix lexer generator template issues 2025-03-10 04:56:55 +03:00
twig_test.go Fix lexer generator template issues 2025-03-10 04:56:55 +03:00

Twig

Twig is a fast, memory-efficient Twig template engine implementation for Go. It aims to provide full support for the Twig template language in a Go-native way.

Features

  • Zero-allocation rendering where possible
  • Full Twig syntax support
  • Template inheritance
  • Extensible with filters, functions, tests, and operators
  • Multiple loader types (filesystem, in-memory)
  • Compatible with Go's standard library interfaces

Installation

go get github.com/semihalev/twig

Basic Usage

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
    }
}

Supported Twig Syntax

  • Variable printing: {{ variable }}
  • Control structures: {% if %}, {% for %}, etc.
  • Filters: {{ variable|filter }}
  • Functions: {{ function(args) }}
  • Template inheritance: {% extends %}, {% block %}
  • Includes: {% include %}
  • Comments: {# comment #}
  • And more...

Performance

The library is designed with performance in mind:

  • Minimal memory allocations
  • Efficient parsing and rendering
  • Template caching

License

This project is licensed under the MIT License - see the LICENSE file for details.