#handlebars #engine #template

rusty-handlebars

Fast compile time handlebars template engine for Rust

2 releases

new 0.1.1 Mar 27, 2025
0.1.0 Mar 17, 2025

#145 in Template engine

Download history 75/week @ 2025-03-12 33/week @ 2025-03-19

108 downloads per month

MIT license

81KB
1.5K SLoC

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-html feature
  • 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 parser feature

Installation

Add this to your Cargo.toml:

[dependencies]
rusty-handlebars = "0.1.0"

For HTML minification support, enable the minify-html feature:

[dependencies]
rusty-handlebars = { version = "0.1.0", features = ["minify-html"] }

Usage

Basic Template Usage

use rusty_handlebars::WithRustyHandlebars;

#[derive(WithRustyHandlebars)]
#[template(path = "templates/hello.hbs")]
struct HelloTemplate {
    name: String,
}

HTML-Safe Output

use rusty_handlebars::AsDisplayHtml;

let html = "<script>alert('xss')</script>";
let safe_html = html.as_display_html().to_string();
// Output: &lt;script&gt;alert(&#x27;xss&#x27;)&lt;/script&gt;

Template Features

  1. Variables: Access struct fields using {{field_name}}
  2. Conditionals: Use {{#if}}...{{/if}} for conditional rendering
  3. Loops: Iterate over collections with {{#each}}...{{/each}}
  4. Helpers: Define custom helpers for complex logic
  5. Partials: Include reusable template parts

Features

parser feature

Enables template parsing functionality. This is optional and can be enabled with:

[dependencies]
rusty-handlebars = { version = "0.1.10", features = ["parser"] }

minify-html feature

Enables HTML minification for reduced output size:

[dependencies]
rusty-handlebars = { version = "0.1.10", features = ["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.

Dependencies

~18MB
~265K SLoC