fixed block inheritance with several levels

This commit is contained in:
Ilya Puchka
2017-11-29 23:03:16 +01:00
parent cf7acea440
commit 93c07e22b1
6 changed files with 39 additions and 13 deletions

View File

@@ -1,14 +1,33 @@
class BlockContext {
class var contextKey: String { return "block_context" }
var blocks: [String: BlockNode]
var blocks: [String: [BlockNode]]
init(blocks: [String: BlockNode]) {
self.blocks = blocks
self.blocks = blocks.mapValues({ [$0] })
}
func push(_ block: BlockNode, forKey blockName: String) {
if var blocks = blocks[blockName] {
blocks.append(block)
self.blocks[blockName] = blocks
} else {
self.blocks[blockName] = [block]
}
}
func pop(_ blockName: String) -> BlockNode? {
return blocks.removeValue(forKey: blockName)
if var blocks = blocks[blockName] {
let block = blocks.removeFirst()
if blocks.isEmpty {
self.blocks.removeValue(forKey: blockName)
} else {
self.blocks[blockName] = blocks
}
return block
} else {
return nil
}
}
}
@@ -70,9 +89,7 @@ class ExtendsNode : NodeType {
blockContext = context
for (key, value) in blocks {
if !blockContext.blocks.keys.contains(key) {
blockContext.blocks[key] = value
}
blockContext.push(value, forKey: key)
}
} else {
blockContext = BlockContext(blocks: blocks)
@@ -109,7 +126,11 @@ class BlockNode : NodeType {
func render(_ context: Context) throws -> String {
if let blockContext = context[BlockContext.contextKey] as? BlockContext, let node = blockContext.pop(name) {
return try context.push(dictionary: ["block": ["super": self]]) {
let newContext: [String: Any] = [
BlockContext.contextKey: blockContext,
"block": ["super": try self.render(context)]
]
return try context.push(dictionary: newContext) {
return try node.render(context)
}
}