@@ -19,6 +19,9 @@
|
||||
index will now resolve to `nil` instead of causing a crash.
|
||||
[#72](https://github.com/kylef/Stencil/issues/72)
|
||||
|
||||
- Templates can now extend templates that extend other templates.
|
||||
[#60](https://github.com/kylef/Stencil/issues/60)
|
||||
|
||||
|
||||
## 0.6.0
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
class BlockContext {
|
||||
class var contextKey: String { return "block_context" }
|
||||
|
||||
var blocks: [String:BlockNode]
|
||||
var blocks: [String: BlockNode]
|
||||
|
||||
init(blocks: [String:BlockNode]) {
|
||||
init(blocks: [String: BlockNode]) {
|
||||
self.blocks = blocks
|
||||
}
|
||||
|
||||
@@ -42,10 +42,9 @@ class ExtendsNode : NodeType {
|
||||
throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")
|
||||
}
|
||||
|
||||
let blockNodes = parsedNodes.filter { node in node is BlockNode }
|
||||
let blockNodes = parsedNodes.flatMap { $0 as? BlockNode }
|
||||
|
||||
let nodes = blockNodes.reduce([String:BlockNode]()) { (accumulator, node:NodeType) -> [String:BlockNode] in
|
||||
let node = (node as! BlockNode)
|
||||
let nodes = blockNodes.reduce([String: BlockNode]()) { (accumulator, node) -> [String: BlockNode] in
|
||||
var dict = accumulator
|
||||
dict[node.name] = node
|
||||
return dict
|
||||
@@ -69,11 +68,23 @@ class ExtendsNode : NodeType {
|
||||
}
|
||||
|
||||
guard let template = loader.loadTemplate(templateName) else {
|
||||
let paths:String = loader.paths.map { $0.description }.joined(separator: ", ")
|
||||
let paths: String = loader.paths.map { $0.description }.joined(separator: ", ")
|
||||
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
||||
}
|
||||
|
||||
let blockContext = BlockContext(blocks: blocks)
|
||||
let blockContext: BlockContext
|
||||
if let context = context[BlockContext.contextKey] as? BlockContext {
|
||||
blockContext = context
|
||||
|
||||
for (key, value) in blocks {
|
||||
if !blockContext.blocks.keys.contains(key) {
|
||||
blockContext.blocks[key] = value
|
||||
}
|
||||
}
|
||||
} else {
|
||||
blockContext = BlockContext(blocks: blocks)
|
||||
}
|
||||
|
||||
return try context.push(dictionary: [BlockContext.contextKey: blockContext]) {
|
||||
return try template.render(context)
|
||||
}
|
||||
@@ -89,7 +100,7 @@ class BlockNode : NodeType {
|
||||
let bits = token.components()
|
||||
|
||||
guard bits.count == 2 else {
|
||||
throw TemplateSyntaxError("'block' tag takes one argument, the template file to be included")
|
||||
throw TemplateSyntaxError("'block' tag takes one argument, the block name")
|
||||
}
|
||||
|
||||
let blockName = bits[1]
|
||||
|
||||
@@ -13,5 +13,11 @@ func testInheritence() {
|
||||
let template = loader.loadTemplate("child.html")
|
||||
try expect(try template?.render(context)) == "Header\nChild"
|
||||
}
|
||||
|
||||
$0.it("can inherit from another template inheriting from another template") {
|
||||
let context = Context(dictionary: ["loader": loader])
|
||||
let template = loader.loadTemplate("child-child.html")
|
||||
try expect(try template?.render(context)) == "Child Child Header\nChild"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
2
Tests/StencilTests/fixtures/child-child.html
Normal file
2
Tests/StencilTests/fixtures/child-child.html
Normal file
@@ -0,0 +1,2 @@
|
||||
{% extends "child.html" %}
|
||||
{% block header %}Child Child Header{% endblock %}
|
||||
Reference in New Issue
Block a user