Oak Handlebars Parser
A high-performance Handlebars template parser for Rust, built with the Oak parser combinator framework. Parse Handlebars templates with comprehensive AST generation and error handling.
Overview
Oak Handlebars provides robust parsing capabilities for Handlebars template files, supporting variables, helpers, partials, block helpers, and all major Handlebars constructs. Built on the Oak parser combinator framework, it delivers excellent performance and detailed error messages.
Features
- ✅ Complete Handlebars Support: Parse variables, helpers, partials, and block helpers
- ✅ 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 Handlebars dialects
- ✅ Well Tested: Comprehensive test suite with real-world examples
Quick Start
Parsing Examples
Basic Template Parsing
use ;
use HandlebarsLanguage;
Advanced Template with Partials and Helpers
use ;
use HandlebarsLanguage;
Advanced Features
Custom Helpers
Oak Handlebars supports parsing custom helper definitions:
let source = r#"
{{#uppercase}}
hello world
{{/uppercase}}
{{#repeat 3}}
<p>Item {{@index}}</p>
{{/repeat}}
"#;
Whitespace Control
Parse templates with whitespace control:
let source = r#"
<ul>
{{~#each items~}}
<li>{{name}}</li>
{{~/each~}}
</ul>
"#;
Subexpressions
Parse complex subexpressions:
let source = r#"
{{#each (filter posts "published")}}
<article>{{title}}</article>
{{/each}}
{{#if (and user.isAdmin (gt posts.length 0))}}
<div>Admin controls here</div>
{{/if}}
"#;
AST Structure
The parser generates a rich AST with the following main node types:
HandlebarsFile- Root node containing the entire templateText- Static text contentVariable- Variable references like {{name}}Helper- Helper calls like {{helper arg}}BlockHelper- Block helpers like {{#each}}...{{/each}}Partial- Partial includes like {{> partial}}Comment- Handlebars comments {{!-- comment --}}Subexpression- Nested subexpressions
Performance
Oak Handlebars 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 Handlebars integrates seamlessly with the Oak ecosystem:
use ;
use HandlebarsLanguage;
// 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.