-
-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First thing that's not None or empty #814
Comments
Currently I have to do this like so, which is verbose:
|
Assuming that So say we have the following struct: #[derive(Template)]
struct Page {
a: Option<String>,
b: Option<String>,
default: String,
} Then to be able to render {{ a.as_deref().unwrap_or(default.as_str()) }} If you want to render {{ a.as_deref().or(b.as_deref()).unwrap_or(default.as_str()) }} Complete Example: #[derive(Template)]
#[template(
source = "{{ a.as_deref().or(b.as_deref()).unwrap_or(default.as_str()) }}",
ext = "html"
)]
struct Page {
a: Option<String>,
b: Option<String>,
default: String,
}
let pages = [
Page {
a: Some("a".into()),
b: None,
default: "default".into(),
},
Page {
a: None,
b: Some("b".into()),
default: "default".into(),
},
Page {
a: Some("a".into()),
b: Some("b".into()),
default: "default".into(),
},
Page {
a: None,
b: None,
default: "default".into(),
},
];
for page in pages {
println!("{:?}", page.render().unwrap());
} Which would output: "a"
"b"
"a"
"default" |
An Option can also be used as a single element iterator, and might come in handy with for-else. |
Is there a way to do something like:
{{ venue.name or venue.fallback_name }}
So take the first thing that's not None or empty and render that?
The text was updated successfully, but these errors were encountered: