From 7b9817ed50b83ad874a6885f939fc1c77ab92193 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Thu, 7 Sep 2017 18:21:16 +0100 Subject: [PATCH] feat(for tag): Provide the counter 0 indexed to the context Closes #121 --- CHANGELOG.md | 5 +++++ Sources/ForTag.swift | 1 + Tests/StencilTests/ForNodeSpec.swift | 6 ++++++ docs/builtins.rst | 21 +++++++++++---------- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5574cce..dcff662 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Master +### Enhancements + +- Adds `counter0` to for loop context allowing you to get the current index of + the for loop 0 indexed. + ### Bug Fixes - Fixes a potential crash when using the `{% for %}` template tag with the diff --git a/Sources/ForTag.swift b/Sources/ForTag.swift index 77228d0..d921a82 100644 --- a/Sources/ForTag.swift +++ b/Sources/ForTag.swift @@ -114,6 +114,7 @@ class ForNode : NodeType { "first": index == 0, "last": index == (count - 1), "counter": index + 1, + "counter0": index, ] return try context.push(dictionary: ["forloop": forContext]) { diff --git a/Tests/StencilTests/ForNodeSpec.swift b/Tests/StencilTests/ForNodeSpec.swift index 3c6cdc8..df77f66 100644 --- a/Tests/StencilTests/ForNodeSpec.swift +++ b/Tests/StencilTests/ForNodeSpec.swift @@ -83,6 +83,12 @@ func testForNode() { try expect(try node.render(context)) == "112233" } + $0.it("renders the given nodes while providing item counter") { + let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.counter0")] + let node = ForNode(resolvable: Variable("items"), loopVariables: ["item"], nodes: nodes, emptyNodes: []) + try expect(try node.render(context)) == "102132" + } + $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())) diff --git a/docs/builtins.rst b/docs/builtins.rst index 7b6d4ea..d0590bb 100644 --- a/docs/builtins.rst +++ b/docs/builtins.rst @@ -57,23 +57,24 @@ The for block sets a few variables available within the loop: - ``first`` - True if this is the first time through the loop - ``last`` - True if this is the last time through the loop -- ``counter`` - The current iteration of the loop +- ``counter`` - The current iteration of the loop (1 indexed) +- ``counter0`` - The current iteration of the loop (0 indexed) -For example: +For example: .. code-block:: html+django - {% for user in users %} - {% if forloop.first %} - This is the first user. - {% endif %} - {% endfor %} + {% for user in users %} + {% if forloop.first %} + This is the first user. + {% endif %} + {% endfor %} .. code-block:: html+django - {% for user in users %} - This is user number {{ forloop.counter }} user. - {% endfor %} + {% for user in users %} + This is user number {{ forloop.counter }} user. + {% endfor %} ``if``