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 committed Jan 1, 2018
1 parent 2e80f70 commit 24765a9
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 @@ -7,6 +7,7 @@
- Added support for resolving superclass properties for not-NSObject subclasses
- The `{% for %}` tag can now iterate over tuples, structures and classes via
their stored properties.
- 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 @@ -131,6 +131,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 @@ -89,6 +89,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 24765a9

Please sign in to comment.