Skip to content

Commit

Permalink
added property for loop length
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka authored and kylef committed Apr 5, 2018
1 parent 6b02fcc commit 96ebfe0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Sources/ForTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
6 changes: 6 additions & 0 deletions Tests/StencilTests/ForNodeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down

0 comments on commit 96ebfe0

Please sign in to comment.