Skip to content

Commit

Permalink
Merge pull request #4 from sinsay/master
Browse files Browse the repository at this point in the history
fix the error of @last keyword
  • Loading branch information
bheisler committed Jun 14, 2019
2 parents ec1e6e3 + c6a5c73 commit 544d7e4
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl<'template> Template<'template> {
}
"@last" => {
let (index, length) = render_context.lookup_index()?;
write!(output, "{}", index == length).unwrap()
write!(output, "{}", index == length - 1).unwrap()
}
_ => panic!(), // This should have been caught by the parser.
}
Expand Down Expand Up @@ -185,7 +185,7 @@ impl<'template> Template<'template> {
"@first" => render_context.lookup_index()?.0 == 0,
"@last" => {
let (index, length) = render_context.lookup_index()?;
index == length
index == (length - 1)
}
_ => panic!(), // This should have been caught by the parser.
}
Expand Down Expand Up @@ -550,6 +550,19 @@ mod test {
assert_eq!("0", &string);
}

#[test]
fn test_for_loop_last() {
let template =
compile("{{ for a in array }}{{ if @last}}{ @index }{{ endif }}{{ endfor }}");
let context = context();
let template_registry = other_templates();
let formatter_registry = formatters();
let string = template
.render(&context, &template_registry, &formatter_registry)
.unwrap();
assert_eq!("2", &string);
}

#[test]
fn test_whitespace_stripping_value() {
let template = compile("1 \n\t {- number -} \n 1");
Expand Down

0 comments on commit 544d7e4

Please sign in to comment.