Oak Twig Parser
A high-performance Twig template parser for Rust, built with the Oak parser combinator framework. Parse Twig templates with comprehensive AST generation and error handling.
Overview
Oak Twig provides robust parsing capabilities for Twig template files, supporting variables, filters, functions, tags, and all major Twig constructs. Built on the Oak parser combinator framework, it delivers excellent performance and detailed error messages.
Features
- ✅ Complete Twig Support: Parse variables, filters, functions, tags, and blocks
- ✅ Modern Rust API: Type-safe parsing with comprehensive error handling
- ✅ High Performance: Built on the efficient Oak parser combinator framework
- ✅ Rich AST: Detailed Abstract Syntax Tree with source location tracking
- ✅ Extensible: Easy to extend for custom Twig dialects
- ✅ Well Tested: Comprehensive test suite with real-world examples
Quick Start
Add Oak Twig to your Cargo.toml:
[]
= "0.1.0"
= "0.1.0"
Parsing Examples
Basic Template Parsing
use ;
use TwigLanguage;
Advanced Template with Inheritance
use ;
use TwigLanguage;
Advanced Features
Custom Functions and Filters
Oak Twig supports parsing custom functions and filters:
let source = r#"
{% set price = calculate_tax(product.price, tax_rate) %}
{% set discounted = apply_discount(price, discount) %}
<p>Original: {{ product.price|currency }}</p>
<p>With tax: {{ price|currency }}</p>
<p>Final: {{ discounted|currency|default('N/A') }}</p>
{% set categories = product.categories|filter(category => category.active) %}
{% set tags = product.tags|map(tag => tag.name)|join(', ') %}
"#;
Macros
Parse Twig macros:
let source = r#"
{% macro input(name, value, type = 'text', size = 20) %}
<input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}" />
{% endmacro %}
{% macro textarea(name, value, rows = 10, cols = 40) %}
<textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols }}">{{ value|e }}</textarea>
{% endmacro %}
{{ input('username', user.name) }}
{{ textarea('bio', user.bio, rows=5) }}
"#;
Conditional Logic
Parse complex conditional logic:
let source = r#"
{% if product.price > 100 and product.category == 'electronics' %}
<span class="badge premium">Premium Product</span>
{% elseif product.sale_price %}
<span class="badge sale">On Sale - Save {{ product.price - product.sale_price|round }}</span>
{% else %}
<span class="badge standard">Standard Product</span>
{% endif %}
{% set class = 'product-card' %}
{% if product.featured %}
{% set class = class ~ ' featured' %}
{% endif %}
{% if product.new %}
{% set class = class ~ ' new' %}
{% endif %}
<div class="{{ class }}">
<!-- product content -->
</div>
"#;
AST Structure
The parser generates a rich AST with the following main node types:
TwigFile- Root node containing the entire templateText- Static text contentVariable- Variable references like {{ name }}Filter- Filter applications like {{ name|upper }}Function- Function calls like {{ date(format='Y-m-d') }}Tag- Twig tags like {% for %}, {% if %}, etc.Block- Template blocks like {% block content %}Comment- Twig comments {# comment #}Expression- Complex expressions and operators
Performance
Oak Twig is designed for high performance:
- Zero-copy parsing where possible
- Streaming support for large template files
- Efficient memory usage with minimal allocations
- Fast error recovery for better developer experience
Integration
Oak Twig integrates seamlessly with the Oak ecosystem:
use ;
use TwigLanguage;
// Use with other Oak parsers
let mut parser = new;
let result = parser.parse;
Examples
More examples can be found in the examples directory:
Contributing
We welcome contributions! Please see our Contributing Guide for details.