feat(block): Support rendering parent block

This commit is contained in:
Kyle Fuller
2016-12-02 00:59:01 +00:00
parent 49936c36d4
commit d75db241ac
4 changed files with 13 additions and 1 deletions

View File

@@ -49,6 +49,9 @@
{% endfilter %}
```
- You can now use `{{ block.super }}` to render a super block from another `{%
block %}`.
### Deprecations
- `Template` initialisers have been deprecated in favour of using a template

View File

@@ -109,8 +109,10 @@ 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]]) {
return try node.render(context)
}
}
return try renderNodes(nodes, context)
}

View File

@@ -18,5 +18,10 @@ func testInheritence() {
let template = try environment.loadTemplate(name: "child-child.html")
try expect(try template.render()) == "Child Child Header\nChild"
}
$0.it("can inherit from a template that calls a super block") {
let template = try environment.loadTemplate(name: "child-super.html")
try expect(try template.render()) == "Header\nChild Body"
}
}
}

View File

@@ -128,6 +128,8 @@ A child template might look like the following:
{% endfor %}
{% endblock %}
.. note:: You can use ``{{ block.super }}` inside a block to render the contents of the parent block inline.
Since our child template doesn't declare a sidebar block. The original sidebar
from our base template will be used. Depending on the content of ``notes`` our
template might be rendered like the following: