https://github.com/nickjer/handlebars_switch
Adds a `{{#switch}}` helper to handlebars-rust
https://github.com/nickjer/handlebars_switch
Last synced: about 1 year ago
JSON representation
Adds a `{{#switch}}` helper to handlebars-rust
- Host: GitHub
- URL: https://github.com/nickjer/handlebars_switch
- Owner: nickjer
- License: mit
- Created: 2017-12-31T17:10:29.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-01-05T13:20:16.000Z (over 2 years ago)
- Last Synced: 2024-04-25T06:20:41.476Z (about 2 years ago)
- Language: Rust
- Size: 14.6 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Handlebars Switch Helper
[](https://crates.io/crates/handlebars_switch)
[](https://crates.io/crates/handlebars_switch)
[](https://github.com/nickjer/handlebars_switch)
[](https://docs.rs/handlebars_switch/)
This provides a [Handlebars](http://handlebarsjs.com/) `{{#switch}}` helper to
the already incredible [handlebars-rust](https://github.com/sunng87/handlebars-rust)
crate.
Links of interest:
- [Documentation](https://docs.rs/handlebars_switch)
- [handlebars-rust](https://github.com/sunng87/handlebars-rust)
- [Handlebars](http://handlebarsjs.com)
## Quick Start
You can easily add the ``{{#switch}}`` helper to a rust Handlebars object using
the `Handlebars#register_helper` method:
```rust
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:
```rust
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"
);
}
```