Skip to content

Releases: semihalev/twig

v1.0.4

25 Jul 23:38

Choose a tag to compare

Bug Fixes

Block Inheritance with Nested Blocks (#1)

  • Fixed block inheritance to work correctly with blocks defined inside control structures
  • Blocks inside {% if %}, {% for %}, {% apply %} and other control structures are now properly recognized and can be extended
  • Added recursive block collection to ensure all blocks are found regardless of nesting level

Template Loading

  • Fixed relative path resolution for template loading
  • Fixed template fallback mechanism to only execute for not found errors (not for syntax errors)

Performance Improvements

Zero Allocation Optimizations

  • Implemented comprehensive zero-allocation rendering path
  • Added buffer pooling to reduce memory allocations
  • Optimized string handling during tokenization with global string cache
  • Improved expression evaluation to reduce allocations
  • Added node pooling for commonly used node types

New Performance Features

  • Introduced zero_alloc_tokenizer.go for high-performance tokenization
  • Added extensive buffer pool implementation for efficient memory reuse
  • Implemented expression pooling to reduce GC pressure

Code Quality Improvements

  • Fixed indentation and formatting issues
  • Renamed parser_include.go to parse_include.go (fixing typo)
  • Removed redundant optimization benchmark files
  • Consolidated optimization implementations

Testing

  • Added comprehensive tests for nested block scenarios
  • Added relative path resolution tests
  • Added benchmarks for buffer pooling and expression evaluation

Breaking Changes

None

Upgrade Notes

This release is fully backward compatible. Users can upgrade without any code changes.

Contributors

Thanks to @simmstein for reporting the block inheritance issue (#1).

v1.0.3

11 Mar 20:19

Choose a tag to compare

Twig v1.0.3

New Features

Security Enhancement

  • Sandbox Mode: Added comprehensive sandbox security system for templates
    • Implemented a SecurityPolicy interface to restrict allowed functions, filters, and tags
    • Added DefaultSecurityPolicy with safe defaults for common operations
    • Added sandboxed option to include tag for secure template inclusion
    • Added engine-level sandbox control methods: EnableSandbox() and DisableSandbox()

Template Inheritance Enhancement

  • Parent Function: Implemented parent() function for accessing parent block content
    • Allows child templates to extend rather than completely replace block content
    • Preserves proper inheritance chain for multi-level inheritance
    • Enhanced block context tracking for proper parent reference resolution

Whitespace Control

  • Dash Modifiers: Added support for whitespace control with dash modifiers (-)
    • Added support for {{- ... }} and {{ ... -}} to trim whitespace before/after output
    • Added support for {%- ... %} and {% ... -%} to trim whitespace before/after block tags
    • Preserves template readability while controlling output formatting

Improvements

  • Code Organization: Enhanced internal APIs for better maintainability
  • Documentation: Updated README and wiki with new features
  • Code Quality: Improved error handling and memory management
  • Performance: Optimized context handling for sandboxed includes

Documentation

  • Updated README with detailed information about new features
  • Added comprehensive wiki pages for Sandbox Mode, Parent Function, and Whitespace Control
  • Updated release notes with detailed information about changes

v1.0.2

11 Mar 15:23

Choose a tag to compare

Twig v1.0.2 Release Notes

New Features

Added New Tags

  • Apply Tag: Implement {% apply filter %}...{% endapply %} tag that applies filters to blocks of content
  • Verbatim Tag: Added {% verbatim %}...{% endverbatim %} tag to output Twig syntax without processing it
  • Do Tag: Implement {% do %} tag for performing expressions without outputting results

Added New Filter

  • Spaceless Filter: Added spaceless filter that removes whitespace between HTML tags

Improvements

  • Path Resolution: Fixed template path resolution for relative paths in templates
    • Properly resolves paths starting with "./" or "../" relative to the current template's directory
    • Enables templates in subdirectories to properly include/extend templates using relative paths
  • Code Organization: Split parser functions into separate files for better maintainability
  • Documentation: Updated README with new tags and filter documentation
  • Code Quality: Cleaned up formatting and removed debug code

Comprehensive Testing

  • Added tests for all new functionality:
    • Verbatim tag tests
    • Apply tag tests
    • Spaceless filter tests
    • From tag tests
    • Relative path resolution tests

Wiki Documentation

  • Added comprehensive wiki pages for better documentation organization

v1.0.1

11 Mar 11:58

Choose a tag to compare

Twig v1.0.1 Release Notes

Overview

Twig v1.0.1 brings significant performance improvements, bug fixes, and new features to the Twig template engine for Go. This release focuses on memory efficiency, rendering speed, and overall stability with impressive benchmark results.

Performance Improvements

  • Object Pooling: Implemented comprehensive object and token pooling for near-zero memory allocation during template rendering
  • Filter Chain Optimization: Dramatically improved filter chain handling with optimized builder patterns and O(n) complexity
  • Attribute Caching: Added efficient LRU eviction strategy for attribute cache to reduce reflection overhead
  • String Handling: Optimized string to byte conversions during rendering for better performance
  • Template Serialization: Optimized compiled template serialization for faster loading and smaller memory footprint
  • Concurrency: Fixed lock contention in template loading for better multi-threaded performance

Bug Fixes

  • Fixed goroutine leaks in render context error paths
  • Fixed array filters and added support for GetItemNode and array access
  • Fixed negative number handling in filter tests
  • Improved regex handling for the matches operator
  • Fixed short-circuit evaluation for logical operators
  • Fixed range function inclusivity and map iteration tests
  • Fixed advanced filters and error condition handling
  • Fixed debug tests and improved debug functionality
  • Fixed code style inconsistencies

New Features and Improvements

  • Added comprehensive macro benchmark tests and updated documentation
  • Added serialization benchmarks and updated results
  • Added HTML whitespace control and formatting enhancements
  • Improved string rendering in scripts and JSON-style object handling
  • Enhanced code style and documentation
  • Improved README header presentation with new logo
  • Added advanced macros examples

Benchmark Results

Latest benchmark runs show dramatic performance improvements:

  • Twig is now 57x faster than Go's html/template for complex templates
  • Memory usage reduced by 90% compared to standard Go templates
  • Performance on medium templates improved to 0.14 µs/op from 0.35 µs/op
  • Simple template rendering improved to 0.28 µs/op from 0.47 µs/op

Breaking Changes

None - This release maintains full compatibility with v1.0.0.

Upgrading

This release is a drop-in replacement for Twig v1.0.0 with no changes required to your code or templates.

Contributors

Full Changelog

For a full list of changes, see the commit history.

v1.0.0

10 Mar 07:52

Choose a tag to compare

v1.0.0: First Stable Release

We're excited to announce the first stable release of Twig, a fast, memory-efficient Twig template engine implementation for Go!

✨ Highlights

  • Zero-allocation rendering where possible for maximum performance
  • Full Twig syntax support including filters, functions, tests, and operators
  • Template inheritance with extends and block tags
  • Multiple loader types including filesystem, in-memory, and compiled templates
  • Thread-safe and concurrency optimized for high-performance applications

🚀 Performance

Twig outperforms other Go template engines in our benchmarks:

  • 33x faster than Go's standard library for complex templates
  • 19x faster than Pongo2 for complex templates
  • 228x faster than Stick for complex templates
  • Uses approximately 33x less memory than Go's standard library

📋 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, compiled)
  • Template compilation for maximum performance
  • Whitespace control features (trim modifiers, spaceless tag)
  • Compatible with Go's standard library interfaces
  • Memory pooling for improved performance
  • Attribute caching to reduce reflection overhead
  • Detailed error reporting and debugging tools
  • Thread-safe and concurrency optimized
  • Robust escape sequence handling in string literals

📝 Documentation

Comprehensive documentation is available in the README, including:

  • Basic usage examples
  • Filter and function documentation
  • Development mode and caching
  • Debugging and error handling
  • String escape sequences and whitespace handling
  • Template compilation guide

🌱 Getting Started

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",
    }
    
    result, err := engine.Render("index.twig", context)
    if err \!= nil {
        fmt.Println("Error:", err)
        return
    }
    
    fmt.Println(result)
}

🙏 Acknowledgements

Thank you to everyone who has contributed to making this release possible!