feat(for loop): added property for loop length (#171)

This commit is contained in:
Ilya Puchka
2018-04-05 01:23:02 +01:00
committed by Kyle Fuller
parent 6b02fccf84
commit f457cddd3f
3 changed files with 8 additions and 0 deletions

View File

@@ -14,6 +14,7 @@
- Allow using new lines inside tags
- Added support for iterating arrays of tuples
- Added support for ranges in if-in expression
- Added property `forloop.length` to get number of items in the loop
### Bug Fixes

View File

@@ -132,6 +132,7 @@ class ForNode : NodeType {
"last": index == (count - 1),
"counter": index + 1,
"counter0": index,
"length": count
]
return try context.push(dictionary: ["forloop": forContext]) {

View File

@@ -90,6 +90,12 @@ func testForNode() {
try expect(try node.render(context)) == "102132"
}
$0.it("renders the given nodes while providing loop length") {
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.length")]
let node = ForNode(resolvable: Variable("items"), loopVariables: ["item"], nodes: nodes, emptyNodes: [])
try expect(try node.render(context)) == "132333"
}
$0.it("renders the given nodes while filtering items using where expression") {
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.counter")]
let `where` = try parseExpression(components: ["item", ">", "1"], tokenParser: TokenParser(tokens: [], environment: Environment()))