| Crates.io | rusty-handlebars-parser |
| lib.rs | rusty-handlebars-parser |
| version | 0.1.3 |
| created_at | 2025-03-17 23:09:57.270795+00 |
| updated_at | 2025-07-22 01:43:53.614678+00 |
| description | Compiler for the rusty-handlebars template engine |
| homepage | |
| repository | https://github.com/h-i-v-e/rusty-handlebars |
| max_upload_size | |
| id | 1595976 |
| size | 91,013 |
A type-safe Handlebars template parser and compiler for Rust. This crate provides the core parsing and compilation functionality for the rusty-handlebars templating engine.
The expression parser handles various types of Handlebars expressions:
{{name}}{{{name}}}{{#helper}}...{{/helper}}{{! comment }}\{{name}}Built-in block helpers include:
if/unless for conditional renderingif_some/if_some_ref for Option handlingwith/with_ref for context changeseach/each_ref for collection iterationThe compiler transforms Handlebars templates into Rust code:
use rusty_handlebars_parser::{Compiler, Options};
use rusty_handlebars_parser::block::add_builtins;
let mut block_map = HashMap::new();
add_builtins(&mut block_map);
let options = Options {
root_var_name: Some("data"),
write_var_name: "write"
};
let compiler = Compiler::new(options, block_map);
let rust = compiler.compile("Hello {{name}}!")?;
let template = r#"
<div class="user-profile">
{{#if user}}
<h1>{{user.name}}</h1>
{{#if user.bio}}
<p class="bio">{{user.bio}}</p>
{{else}}
<p class="no-bio">No bio available</p>
{{/if}}
{{#if_some user.posts as post}}
<div class="posts">
<h2>Posts</h2>
{{#each post as post}}
<article class="post">
<h3>{{post.title}}</h3>
<p>{{post.content}}</p>
<div class="meta">
<span>Posted on {{post.date}}</span>
{{#if post.tags}}
<div class="tags">
{{#each post.tags as tag}}
<span class="tag">{{tag}}</span>
{{/each}}
</div>
{{/if}}
</div>
</article>
{{/each}}
</div>
{{/if_some}}
{{else}}
<p>Please log in to view your profile</p>
{{/if}}
</div>
"#;
let rust = compiler.compile(template)?;
When the minify-html feature is enabled, the parser can optimize HTML output:
#[cfg(feature = "minify-html")]
use rusty_handlebars_parser::build_helper::COMPRESS_CONFIG;
The minification configuration:
The parser provides detailed error information:
use rusty_handlebars_parser::error::{Result, ParseError};
fn compile_template(template: &str) -> Result<String> {
let compiler = Compiler::new(options, block_map);
compiler.compile(template)
}
compiler.rs: Core compilation logicblock.rs: Block helper implementationsexpression.rs: Expression parsingexpression_tokenizer.rs: Tokenization of expressionserror.rs: Error handlingbuild_helper.rs: Build-time configurationContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.