handlebars_switch

Crates.iohandlebars_switch
lib.rshandlebars_switch
version0.7.0
sourcesrc
created_at2017-12-31 17:23:33.487567
updated_at2024-07-22 11:58:54.57755
descriptionAdds a `{{#switch}}` helper to handlebars-rust.
homepagehttps://github.com/nickjer/handlebars_switch
repositoryhttps://github.com/nickjer/handlebars_switch
max_upload_size
id45061
size17,012
Jeremy Nicklas (nickjer)

documentation

https://docs.rs/handlebars_switch/

README

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"
  );
}
Commit count: 15

cargo fmt