diff --git a/CHANGELOG.md b/CHANGELOG.md index b02a7da..babd93a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Sources/Inheritence.swift b/Sources/Inheritence.swift index 8323d00..9919c16 100644 --- a/Sources/Inheritence.swift +++ b/Sources/Inheritence.swift @@ -109,7 +109,9 @@ class BlockNode : NodeType { func render(_ context: Context) throws -> String { if let blockContext = context[BlockContext.contextKey] as? BlockContext, let node = blockContext.pop(name) { - return try node.render(context) + return try context.push(dictionary: ["block": ["super": self]]) { + return try node.render(context) + } } return try renderNodes(nodes, context) diff --git a/Tests/StencilTests/InheritenceSpec.swift b/Tests/StencilTests/InheritenceSpec.swift index 43ed59f..6b741ae 100644 --- a/Tests/StencilTests/InheritenceSpec.swift +++ b/Tests/StencilTests/InheritenceSpec.swift @@ -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" + } } } diff --git a/docs/templates.rst b/docs/templates.rst index bd7d5e1..1934abe 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -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: