diff options
author | René Kijewski <kijewski@library.vetmed.fu-berlin.de> | 2021-07-18 16:32:49 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2021-11-11 15:35:45 +0100 |
commit | 10b2d9c615460c9dbb241b887d0e6e17e3c001ca (patch) | |
tree | 4ec234cf416f7bcdc0bc037f787ebbd7ac4cb67e /testing | |
parent | a8503e0fa2d6065b1c471becf76dde68571b7984 (diff) | |
download | askama-10b2d9c615460c9dbb241b887d0e6e17e3c001ca.tar.gz askama-10b2d9c615460c9dbb241b887d0e6e17e3c001ca.tar.bz2 askama-10b2d9c615460c9dbb241b887d0e6e17e3c001ca.zip |
Implement for-else
This PR implements for-else statements like in Jinja. They make it easy
to print an alternative message if the loop iterator was empty. E.g.
```rs
{% for result in result %}
<li>{{ result }}</li>
{% else %}
<li><em>no results</em></li>
{% endfor %}
```
Diffstat (limited to '')
-rw-r--r-- | testing/tests/loop_else.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/testing/tests/loop_else.rs b/testing/tests/loop_else.rs new file mode 100644 index 0000000..99dfc4b --- /dev/null +++ b/testing/tests/loop_else.rs @@ -0,0 +1,19 @@ +use askama::Template; + +#[derive(Template)] +#[template( + source = "{% for v in values %}{{ v }}{% else %}empty{% endfor %}", + ext = "txt" +)] +struct ForElse<'a> { + values: &'a [i32], +} + +#[test] +fn test_for_else() { + let t = ForElse { values: &[1, 2, 3] }; + assert_eq!(t.render().unwrap(), "123"); + + let t = ForElse { values: &[] }; + assert_eq!(t.render().unwrap(), "empty"); +} |