| Crates.io | handlebars_switch |
| lib.rs | handlebars_switch |
| version | 0.7.0 |
| created_at | 2017-12-31 17:23:33.487567+00 |
| updated_at | 2024-07-22 11:58:54.57755+00 |
| description | Adds a `{{#switch}}` helper to handlebars-rust. |
| homepage | https://github.com/nickjer/handlebars_switch |
| repository | https://github.com/nickjer/handlebars_switch |
| max_upload_size | |
| id | 45061 |
| size | 17,012 |
This provides a Handlebars {{#switch}} helper to
the already incredible handlebars-rust
crate.
Links of interest:
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));
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"
);
}