Access outer loop context via its label

This commit is contained in:
Ilya Puchka
2017-12-29 00:14:26 +01:00
committed by David Jennes
parent 91df84b1a5
commit dd7ea1e097
2 changed files with 51 additions and 0 deletions

View File

@@ -96,6 +96,10 @@ class ForNode: NodeType {
let count = values.count
var result = ""
// collect parent loop contexts
let parentLoopContexts = (context["forloop"] as? [String: Any])?
.filter { ($1 as? [String: Any])?["label"] != nil } ?? [:]
for (index, item) in zip(0..., values) {
var forContext: [String: Any] = [
"first": index == 0,
@@ -106,7 +110,9 @@ class ForNode: NodeType {
]
if let label = label {
forContext["label"] = label
forContext[label] = forContext
}
forContext.merge(parentLoopContexts) { lhs, _ in lhs }
var shouldBreak = false
result += try context.push(dictionary: ["forloop": forContext]) {