Skip to main content

Crate handlebars_switch

Crate handlebars_switch 

Source
Expand description

§Handlebars Switch Helper

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"
    );
}

Structs§

SwitchHelper
Switch Helper