#handlebars #templating #web

handlebars_switch

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

7 releases (breaking)

0.7.0 Jul 22, 2024
0.6.0 Jan 5, 2024
0.5.0 Jul 1, 2022
0.4.0 May 3, 2021
0.1.0 Dec 31, 2017

#67 in Template engine

Download history 180/week @ 2024-07-28 221/week @ 2024-08-04 172/week @ 2024-08-11 257/week @ 2024-08-18 369/week @ 2024-08-25 255/week @ 2024-09-01 281/week @ 2024-09-08 232/week @ 2024-09-15 262/week @ 2024-09-22 268/week @ 2024-09-29 254/week @ 2024-10-06 370/week @ 2024-10-13 327/week @ 2024-10-20 307/week @ 2024-10-27 296/week @ 2024-11-03 377/week @ 2024-11-10

1,308 downloads per month
Used in dot-silo

MIT license

15KB
235 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 rust Handlebars object using the Handlebars#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:

extern crate handlebars_switch;
extern crate handlebars;
#[macro_use] extern crate serde_json;

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

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.template_render(tpl, &json!({"access": "admin"})).unwrap(),
      "Admin"
  );

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

Dependencies

~3–4MB
~87K SLoC