Skip to content

Commit

Permalink
feat(block): Support rendering parent block
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Dec 2, 2016
1 parent 49936c3 commit d75db24
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion Sources/Inheritence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions Tests/StencilTests/InheritenceSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
2 changes: 2 additions & 0 deletions docs/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d75db24

Please sign in to comment.