Skip to content

Commit

Permalink
Add support for indexing into iterables (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Aug 26, 2024
1 parent 3559330 commit 5446f88
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ All notable changes to MiniJinja are documented here.
- Added `filesizeformat` to minijinja-contrib. #556
- Added support for the `loop_controls` feature which adds
`{% break %}` and `{% continue %}`. #558
- Iterables can now be indexed into. It was already possible previously
to slice them. This improves support for Jinja2 compatibility as Jinja2
is more likely to create temporary lists when slicing lists. #565

## 2.1.2

Expand Down
18 changes: 17 additions & 1 deletion minijinja/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,23 @@ impl Value {

match self.0 {
ValueRepr::Object(ref dy) => match dy.repr() {
ObjectRepr::Map | ObjectRepr::Plain | ObjectRepr::Iterable => dy.get_value(key),
ObjectRepr::Map | ObjectRepr::Plain => dy.get_value(key),
ObjectRepr::Iterable => {
if let Some(rv) = dy.get_value(key) {
return Some(rv);
}
// The default behavior is to try to index into the iterable
// as if nth() was called. This lets one slice an array and
// then index into it.
if let Some(idx) = index(key, || dy.enumerator_len()) {
if let Some(mut iter) = dy.try_iter() {
if let Some(rv) = iter.nth(idx) {
return Some(rv);
}
}
}
None
}
ObjectRepr::Seq => {
let idx = index(key, || dy.enumerator_len()).map(Value::from);
dy.get_value(idx.as_ref().unwrap_or(key))
Expand Down
2 changes: 2 additions & 0 deletions minijinja/tests/inputs/slicing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
{{ intrange[::2] }}
{{ intrange[2:10] }}
{{ intrange[2:10:2] }}
{{ intrange[2:10][0] }}
{{ intrange[2:10][2:][0] }}
5 changes: 3 additions & 2 deletions minijinja/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: minijinja/tests/test_templates.rs
description: "{{ hello[:] }}\n{{ hello[1:] }}\n{{ hello[1:-1] }}\n{{ hello[::2] }}\n{{ hello[2:10] }}\n{{ hello[2:10:2] }}\n{{ intrange[:] }}\n{{ intrange[1:] }}\n{{ intrange[1:-1] }}\n{{ intrange[::2] }}\n{{ intrange[2:10] }}\n{{ intrange[2:10:2] }}"
description: "{{ hello[:] }}\n{{ hello[1:] }}\n{{ hello[1:-1] }}\n{{ hello[::2] }}\n{{ hello[2:10] }}\n{{ hello[2:10:2] }}\n{{ intrange[:] }}\n{{ intrange[1:] }}\n{{ intrange[1:-1] }}\n{{ intrange[::2] }}\n{{ intrange[2:10] }}\n{{ intrange[2:10:2] }}\n{{ intrange[2:10][0] }}\n{{ intrange[2:10][2:][0] }}"
info:
hello: Hällo Wörld
intrange:
Expand Down Expand Up @@ -28,4 +28,5 @@ loWr
[0, 2, 4, 6, 8]
[2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8]

2
4

0 comments on commit 5446f88

Please sign in to comment.