Cache rendered blocks content to reuse them in further calls

This commit is contained in:
Ilya Puchka
2017-12-30 03:02:18 +01:00
committed by David Jennes
parent 4d3f911f5d
commit 8c379296ca
5 changed files with 44 additions and 1 deletions

View File

@@ -85,4 +85,19 @@ public class Context {
return accumulator
}
/// Cache result of block by its name in the context top-level, so that it can be later rendered
/// via `{{ block.name }}`
///
/// - Parameters:
/// - name: The name of the stored block
/// - content: The block's rendered content
public func cacheBlock(_ name: String, content: String) {
if var block = dictionaries.first?["block"] as? [String: String] {
block[name] = content
dictionaries[0]["block"] = block
} else {
dictionaries.insert(["block": [name: content]], at: 0)
}
}
}

View File

@@ -151,6 +151,8 @@ class BlockNode: NodeType {
}
}
return try renderNodes(nodes, context)
let result = try renderNodes(nodes, context)
context.cacheBlock(name, content: result)
return result
}
}