feat(for loop): added property for loop length (#171)
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
- Allow using new lines inside tags
|
- Allow using new lines inside tags
|
||||||
- Added support for iterating arrays of tuples
|
- Added support for iterating arrays of tuples
|
||||||
- Added support for ranges in if-in expression
|
- Added support for ranges in if-in expression
|
||||||
|
- Added property `forloop.length` to get number of items in the loop
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|
||||||
|
|||||||
@@ -132,6 +132,7 @@ class ForNode : NodeType {
|
|||||||
"last": index == (count - 1),
|
"last": index == (count - 1),
|
||||||
"counter": index + 1,
|
"counter": index + 1,
|
||||||
"counter0": index,
|
"counter0": index,
|
||||||
|
"length": count
|
||||||
]
|
]
|
||||||
|
|
||||||
return try context.push(dictionary: ["forloop": forContext]) {
|
return try context.push(dictionary: ["forloop": forContext]) {
|
||||||
|
|||||||
@@ -90,6 +90,12 @@ func testForNode() {
|
|||||||
try expect(try node.render(context)) == "102132"
|
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") {
|
$0.it("renders the given nodes while filtering items using where expression") {
|
||||||
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.counter")]
|
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.counter")]
|
||||||
let `where` = try parseExpression(components: ["item", ">", "1"], tokenParser: TokenParser(tokens: [], environment: Environment()))
|
let `where` = try parseExpression(components: ["item", ">", "1"], tokenParser: TokenParser(tokens: [], environment: Environment()))
|
||||||
|
|||||||
Reference in New Issue
Block a user