Rusty Handlebars
A Rust implementation of Handlebars templating engine with a focus on type safety and compile-time template processing.
Features
- Type-safe templating: Compile-time template processing with Rust's type system
- HTML escaping: Built-in HTML escaping for secure output
- Optional HTML minification: Reduce HTML output size with the
minify-htmlfeature - Derive macro support: Easy integration with Rust structs using
#[derive(WithRustyHandlebars)] - Flexible display traits: Support for both regular and HTML-safe display implementations
- Optional parser: Template parsing functionality available via the
parserfeature
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
For HTML minification support, enable the minify-html feature:
[]
= { = "0.1.0", = ["minify-html"] }
Usage
Basic Template Usage
use WithRustyHandlebars;
HTML-Safe Output
use AsDisplayHtml;
let html = "<script>alert('xss')</script>";
let safe_html = html.as_display_html.to_string;
// Output: <script>alert('xss')</script>
Template Features
- Variables: Access struct fields using
{{field_name}} - Conditionals: Use
{{#if}}...{{/if}}for conditional rendering - Loops: Iterate over collections with
{{#each}}...{{/each}} - Helpers: Define custom helpers for complex logic
- Partials: Include reusable template parts
Features
parser feature
Enables template parsing functionality. This is optional and can be enabled with:
[]
= { = "0.1.10", = ["parser"] }
minify-html feature
Enables HTML minification for reduced output size:
[]
= { = "0.1.10", = ["minify-html"] }
Safety
- All HTML output is automatically escaped by default
- Type-safe template processing prevents runtime errors
- Compile-time template validation
- No unsafe code in the main crate
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.