Handlebars templating language implemented in Rust and for Rust.
extern crate handlebars; extern crate serde_json; use Handlebars;
If you are not familiar with handlebars language syntax, it is recommended to walk through their introduction first.
render example in the source tree. The example shows you how
- Create a
Handlebarsregistry and register the template from files;
- Create a custom Helper with closure or struct implementing
HelperDef, and register it;
- Define and prepare some data;
- Render it;
cargo run --example render to see results
RUST_LOG=handlebars=info cargo run --example render for logging
examples/ for more concrete demos of the current API.
Minimum Rust Version Policy
Handlebars will track Rust nightly and stable channel. When dropping support for previous stable versions, I will bump minor version and clarify in CHANGELOG.
Rust compatibility table
|Handlebars version range||Minimum Rust version|
Changelog is available in the source tree named as
Any contribution to this library is welcomed. To get started into development, I have several Help Wanted issues, with the difficulty level labeled. When running into any problem, feel free to contact me on github.
I'm always looking for maintainers to work together on this library, let me know (via email or anywhere in the issue tracker) if you want to join.
Why (this) Handlebars?
Handlebars is a real-world templating system that you can use to build your application without pain.
Isolation of Rust and HTML
This library doesn't attempt to use some macro magic to allow you to write your template within your rust code. I admit that it's fun to do that but it doesn't fit real-world use cases.
Limited but essential control structures built-in
Only essential control directives
each are built-in. This
prevents you from putting too much application logic into your template.
Extensible helper system
You can write your own helper with Rust! It can be a block helper or inline helper. Put your logic into the helper and don't repeat yourself.
A helper can be as a simple as a Rust function like:
handlebars_helper!; /// register the helper handlebars.register_helper;
And using it in your template:
script_helper feature flag enabled, you can also create helpers
for handlebars-js. This feature was in early stage. Its API was limited at the
moment, and can change in future.
Every time I look into a templating system, I will investigate its support for template inheritance.
Template include is not sufficient for template reuse. In most cases you will need a skeleton of page as parent (header, footer, etc.), and embed your page into this parent.
You can find a real example of template inheritance in
examples/partials.rs and templates used by this file.
Handlebars 3.0 can be used in WebAssembly projects.
- Iron: handlebars-iron
- Rocket: rocket/contrib
- Warp: handlebars example
- Tower-web: Built-in
- Actix: handlebars example
- Tide: tide-handlebars
The adopters page lists projects that uses handlebars for part of their functionalities.
The extensions page has libraries that provide additional helpers, decorators and outputs to handlebars-rust, and you can use in your own projects.
This library (handlebars-rust) is open sourced under the MIT License.