#handlebars #templating #web

handlebars_switch

Adds a {{#switch}} helper to handlebars-rust

9 releases (2 stable)

Uses new Rust 2024

1.0.2 Apr 12, 2026
0.7.0 Jul 22, 2024
0.6.0 Jan 5, 2024
0.5.0 Jul 1, 2022
0.1.0 Dec 31, 2017

#144 in Template engine

Download history 243/week @ 2026-02-07 160/week @ 2026-02-14 371/week @ 2026-02-21 206/week @ 2026-02-28 267/week @ 2026-03-07 849/week @ 2026-03-14 477/week @ 2026-03-21 480/week @ 2026-03-28 208/week @ 2026-04-04 223/week @ 2026-04-11 680/week @ 2026-04-18 300/week @ 2026-04-25 197/week @ 2026-05-02 307/week @ 2026-05-09 274/week @ 2026-05-16 244/week @ 2026-05-23

1,029 downloads per month
Used in dot-silo

MIT license

23KB
484 lines

Handlebars Switch Helper

Latest Version Downloads License Docs

This provides a Handlebars {{#switch}} helper to the already incredible handlebars-rust crate.

Links of interest:

Quick Start

You can easily add the {{#switch}} helper to a Handlebars instance using the register_helper method:

use handlebars::Handlebars;
use handlebars_switch::SwitchHelper;

let mut handlebars = Handlebars::new();
handlebars.register_helper("switch", Box::new(SwitchHelper));

Example

Below is an example that renders a different page depending on the user's access level:

use handlebars::Handlebars;
use handlebars_switch::SwitchHelper;
use serde_json::json;

fn main() {
    let mut handlebars = Handlebars::new();
    handlebars.register_helper("switch", Box::new(SwitchHelper));

    let tpl = "\
        {{#switch access}}\
            {{#case \"admin\"}}Admin{{/case}}\
            {{#default}}User{{/default}}\
        {{/switch}}\
    ";

    assert_eq!(
        handlebars.render_template(tpl, &json!({"access": "admin"})).unwrap(),
        "Admin"
    );

    assert_eq!(
        handlebars.render_template(tpl, &json!({"access": "nobody"})).unwrap(),
        "User"
    );
}

Dependencies

~4.5MB
~91K SLoC