From 10b2d9c615460c9dbb241b887d0e6e17e3c001ca Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Sun, 18 Jul 2021 16:32:49 +0200 Subject: 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 %}
  • {{ result }}
  • {% else %}
  • no results
  • {% endfor %} ``` --- testing/tests/loop_else.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 testing/tests/loop_else.rs (limited to 'testing/tests/loop_else.rs') 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"); +} -- cgit