feat(block): Support rendering parent block
This commit is contained in:
@@ -49,6 +49,9 @@
|
|||||||
{% endfilter %}
|
{% endfilter %}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- You can now use `{{ block.super }}` to render a super block from another `{%
|
||||||
|
block %}`.
|
||||||
|
|
||||||
### Deprecations
|
### Deprecations
|
||||||
|
|
||||||
- `Template` initialisers have been deprecated in favour of using a template
|
- `Template` initialisers have been deprecated in favour of using a template
|
||||||
|
|||||||
@@ -109,8 +109,10 @@ class BlockNode : NodeType {
|
|||||||
|
|
||||||
func render(_ context: Context) throws -> String {
|
func render(_ context: Context) throws -> String {
|
||||||
if let blockContext = context[BlockContext.contextKey] as? BlockContext, let node = blockContext.pop(name) {
|
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 node.render(context)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return try renderNodes(nodes, context)
|
return try renderNodes(nodes, context)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,5 +18,10 @@ func testInheritence() {
|
|||||||
let template = try environment.loadTemplate(name: "child-child.html")
|
let template = try environment.loadTemplate(name: "child-child.html")
|
||||||
try expect(try template.render()) == "Child Child Header\nChild"
|
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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,6 +128,8 @@ A child template might look like the following:
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endblock %}
|
{% 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
|
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
|
from our base template will be used. Depending on the content of ``notes`` our
|
||||||
template might be rendered like the following:
|
template might be rendered like the following:
|
||||||
|
|||||||
Reference in New Issue
Block a user