mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-30 06:21:13 +00:00
Fix operator preference at loop.first
This commit is contained in:
parent
d467161159
commit
c0f9fe973e
@ -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");
|
||||
|
3
testing/templates/precedence-for.html
Normal file
3
testing/templates/precedence-for.html
Normal file
@ -0,0 +1,3 @@
|
||||
{% for s in strings %}
|
||||
{{- loop.index0 }}. {{ s }}{% if !loop.first %}{% else %} (first){% endif %}
|
||||
{% endfor %}
|
@ -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");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user