#handlebars #templating #web


Handlebars templating implemented in Rust

119 releases (10 stable)

✓ Uses Rust 2018 edition

2.0.2 Sep 5, 2019
2.0.1 Jul 12, 2019
2.0.0-beta.3 Jun 24, 2019
2.0.0-beta.1 Mar 16, 2019
0.3.2 Dec 31, 2014

#1 in Template engine

Download history 26188/week @ 2019-05-23 28854/week @ 2019-05-30 32790/week @ 2019-06-06 32650/week @ 2019-06-13 32095/week @ 2019-06-20 23375/week @ 2019-06-27 18837/week @ 2019-07-04 22037/week @ 2019-07-11 18227/week @ 2019-07-18 16790/week @ 2019-07-25 18003/week @ 2019-08-01 19670/week @ 2019-08-08 17471/week @ 2019-08-15 15073/week @ 2019-08-22 15176/week @ 2019-08-29

90,482 downloads per month
Used in 229 crates (108 directly)

MIT license

5.5K SLoC


Handlebars templating language implemented in Rust and for Rust.

Handlebars-rust is the template engine renders Rust official website rust-lang.org and its book.

Build Status MIT licensed Docs Donate

Getting Started

Quick Start

extern crate handlebars;
extern crate serde_json;

use handlebars::Handlebars;

fn main() -> Result<(), Box<dyn Error>> {
    let mut reg = Handlebars::new();
    // render without register
        reg.render_template("Hello {{name}}", &json!({"name": "foo"}))?

    // register template using given name
    reg.register_template_string("tpl_1", "Good afternoon, {{name}}")?;
    println!("{}", reg.render("tpl_1", &json!({"name": "foo"}))?);

Code Example

If you are not familiar with handlebars language syntax, it is recommended to walk through their introduction first.

Check render example in the source tree. The example shows you how to:

  • Create a Handlebars registry 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;

Run cargo run --example render to see results. (or RUST_LOG=handlebars=info cargo run --example render for logging output).

Checkout examples/ for more concrete demos of 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
~2.0.0 1.32
~1.1.0 1.30
~1.0.0 1.23


Rust doc.


Change log is available in the source tree named as CHANGELOG.md.

Contributor Guide

Any contribution to this library is welcomed. To get started into development, I have several Help Wanted issue, with difficult 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, also let me know (via email or anywhere in the issue tracker) if you want to join.


I'm now accepting donation on liberapay, if you find my work helpful and want to keep it going.

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 case.

Limited but essential control structure built-in

Only essential control directive if and each were built-in. This prevents you to put 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!(hex: |v: i64| format!("0x{:x}", v));

/// register the helper
handlebars.register_helper("hex", Box::new(hex));

And using it in your template:

{{hex 16}}

Template inheritance

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 case you will need a skeleton of page as parent (header, footer, etc.), and embed you page into this parent.

You can find a real example for template inheritance in examples/partials.rs, and templates used by this file.

WebAssembly compatible

Handlebars can be used in WebAssembly projects with directory source feature disabled. Adding handlebars to your project like this:

handlebars = { version = "2", features = ["no_dir_source"], default-features = false }

Handlebars for Web Frameworks

Using handlebars-rust?

Add your project to our adopters.


This library (handlebars-rust) is open sourced under MIT License.


~61K SLoC