aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar bott <mhpoin@gmail.com>2018-09-13 23:40:17 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2018-09-14 13:15:41 +0200
commitc0f9fe973e6ff608f4f02f8a6cd39f2e9c50ea5c (patch)
tree9fde9771eec608640d008fed7433c9857aa5c50d
parentd4671611597837dec3108f3a7f3af46be1bc4c36 (diff)
downloadaskama-c0f9fe973e6ff608f4f02f8a6cd39f2e9c50ea5c.tar.gz
askama-c0f9fe973e6ff608f4f02f8a6cd39f2e9c50ea5c.tar.bz2
askama-c0f9fe973e6ff608f4f02f8a6cd39f2e9c50ea5c.zip
Fix operator preference at loop.first
Diffstat (limited to '')
-rw-r--r--askama_derive/src/generator.rs6
-rw-r--r--testing/templates/precedence-for.html3
-rw-r--r--testing/tests/loops.rs14
3 files changed, 20 insertions, 3 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index 5a136fb..d0ac030 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -747,14 +747,14 @@ impl<'a> Generator<'a> {
fn visit_attr(&mut self, buf: &mut Buffer, obj: &Expr, attr: &str) -> DisplayWrap {
if let Expr::Var(name) = *obj {
if name == "loop" {
- buf.write("_loop_index");
if attr == "index" {
- buf.write(" + 1");
+ buf.write("_loop_index + 1");
return DisplayWrap::Unwrapped;
} else if attr == "index0" {
+ buf.write("_loop_index");
return DisplayWrap::Unwrapped;
} else if attr == "first" {
- buf.write(" == 0");
+ buf.write("(_loop_index == 0)");
return DisplayWrap::Unwrapped;
} else {
panic!("unknown loop variable");
diff --git a/testing/templates/precedence-for.html b/testing/templates/precedence-for.html
new file mode 100644
index 0000000..e5f7e8b
--- /dev/null
+++ b/testing/templates/precedence-for.html
@@ -0,0 +1,3 @@
+{% for s in strings %}
+ {{- loop.index0 }}. {{ s }}{% if !loop.first %}{% else %} (first){% endif %}
+{% endfor %}
diff --git a/testing/tests/loops.rs b/testing/tests/loops.rs
index ac19d43..6c2f35d 100644
--- a/testing/tests/loops.rs
+++ b/testing/tests/loops.rs
@@ -32,3 +32,17 @@ fn test_nested_for() {
};
assert_eq!(s.render().unwrap(), "1\n 0a1b2c2\n 0one1two");
}
+
+#[derive(Template)]
+#[template(path = "precedence-for.html")]
+struct PrecedenceTemplate<'a> {
+ strings: Vec<&'a str>,
+}
+
+#[test]
+fn test_precedence_for() {
+ let s = PrecedenceTemplate {
+ strings: vec!["A", "alfa", "1"],
+ };
+ assert_eq!(s.render().unwrap(), "0. A (first)\n1. alfa\n2. 1\n");
+}