| Crates.io | oak-handlebars |
| lib.rs | oak-handlebars |
| version | 0.0.1 |
| created_at | 2025-10-21 00:14:39.859104+00 |
| updated_at | 2026-01-23 04:28:54.241809+00 |
| description | Handlebars template engine parser with support for modern templating features and extensions. |
| homepage | https://github.com/ygg-lang/oaks |
| repository | https://github.com/ygg-lang/oaks |
| max_upload_size | |
| id | 1892938 |
| size | 106,291 |
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.
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.
use oak::{Parser, Language};
use oak_handlebars::HandlebarsLanguage;
fn main() {
let source = r#"
<div class="user-profile">
<h1>{{user.name}}</h1>
<p>{{user.bio}}</p>
<ul>
{{#each user.skills}}
<li>{{this}}</li>
{{/each}}
</ul>
</div>
"#;
let mut parser = Parser::<HandlebarsLanguage>::new();
match parser.parse(&source) {
Ok(ast) => {
println!("Parsed AST: {:#?}", ast);
}
Err(error) => {
eprintln!("Parse error: {}", error);
}
}
}
use oak::{Parser, Language};
use oak_handlebars::HandlebarsLanguage;
fn main() {
let source = r#"
{{> header title="My Blog"}}
<main>
{{#if posts}}
<h1>Recent Posts</h1>
{{#each posts}}
<article>
<h2><a href="/posts/{{id}}">{{title}}</a></h2>
<p class="meta">
By {{author.name}} on {{formatDate date}}
</p>
<div class="excerpt">
{{truncate content 200}}
</div>
<a href="/posts/{{id}}">Read more...</a>
</article>
{{/each}}
{{#if showPagination}}
{{> pagination current=currentPage total=totalPages}}
{{/if}}
{{else}}
<p>No posts found.</p>
{{/if}}
</main>
{{> footer}}
"#;
let mut parser = Parser::<HandlebarsLanguage>::new();
match parser.parse(&source) {
Ok(ast) => {
println!("Advanced template parsed successfully!");
}
Err(error) => {
eprintln!("Parse error: {}", error);
}
}
}
Oak Handlebars supports parsing custom helper definitions:
let source = r#"
{{#uppercase}}
hello world
{{/uppercase}}
{{#repeat 3}}
<p>Item {{@index}}</p>
{{/repeat}}
"#;
Parse templates with whitespace control:
let source = r#"
<ul>
{{~#each items~}}
<li>{{name}}</li>
{{~/each~}}
</ul>
"#;
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}}
"#;
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 subexpressionsOak Handlebars is designed for high performance:
Oak Handlebars integrates seamlessly with the Oak ecosystem:
use oak::{Parser, Language};
use oak_handlebars::HandlebarsLanguage;
// Use with other Oak parsers
let mut parser = Parser::<HandlebarsLanguage>::new();
let result = parser.parse(handlebars_source);
More examples can be found in the examples directory:
We welcome contributions! Please see our Contributing Guide for details.