Merge pull request #267 from stencilproject/fix-block-super
Always evaluate `block.super` even if it's not directly used
This commit is contained in:
@@ -19,7 +19,10 @@ _None_
|
|||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|
||||||
_None_
|
- Fixed using `{{ block.super }}` inside nodes other than `block`.
|
||||||
|
[Ilya Puchka](https://github.com/ilyapuchka)
|
||||||
|
[#266](https://github.com/stencilproject/Stencil/issues/266)
|
||||||
|
[#267](https://github.com/stencilproject/Stencil/pull/267)
|
||||||
|
|
||||||
### Internal Changes
|
### Internal Changes
|
||||||
|
|
||||||
|
|||||||
@@ -136,7 +136,11 @@ class BlockNode: NodeType {
|
|||||||
|
|
||||||
func render(_ context: Context) throws -> String {
|
func render(_ context: Context) throws -> String {
|
||||||
if let blockContext = context[BlockContext.contextKey] as? BlockContext, let child = blockContext.pop(name) {
|
if let blockContext = context[BlockContext.contextKey] as? BlockContext, let child = blockContext.pop(name) {
|
||||||
let childContext = try self.childContext(child, blockContext: blockContext, context: context)
|
let childContext: [String: Any] = [
|
||||||
|
BlockContext.contextKey: blockContext,
|
||||||
|
"block": ["super": try self.render(context)]
|
||||||
|
]
|
||||||
|
|
||||||
// render extension node
|
// render extension node
|
||||||
do {
|
do {
|
||||||
return try context.push(dictionary: childContext) {
|
return try context.push(dictionary: childContext) {
|
||||||
@@ -149,35 +153,4 @@ class BlockNode: NodeType {
|
|||||||
|
|
||||||
return try renderNodes(nodes, context)
|
return try renderNodes(nodes, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
// child node is a block node from child template that extends this node (has the same name)
|
|
||||||
func childContext(_ child: BlockNode, blockContext: BlockContext, context: Context) throws -> [String: Any] {
|
|
||||||
var childContext: [String: Any] = [BlockContext.contextKey: blockContext]
|
|
||||||
|
|
||||||
if let blockSuperNode = child.nodes.first(where: {
|
|
||||||
if let token = $0.token, case .variable = token.kind, token.contents == "block.super" {
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}) {
|
|
||||||
do {
|
|
||||||
// render base node so that its content can be used as part of child node that extends it
|
|
||||||
childContext["block"] = ["super": try self.render(context)]
|
|
||||||
} catch {
|
|
||||||
if let error = error as? TemplateSyntaxError {
|
|
||||||
throw TemplateSyntaxError(
|
|
||||||
reason: error.reason,
|
|
||||||
token: blockSuperNode.token,
|
|
||||||
stackTrace: error.allTokens)
|
|
||||||
} else {
|
|
||||||
throw TemplateSyntaxError(
|
|
||||||
reason: "\(error)",
|
|
||||||
token: blockSuperNode.token,
|
|
||||||
stackTrace: [])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return childContext
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -320,7 +320,7 @@ final class EnvironmentBaseAndChildTemplateTests: XCTestCase {
|
|||||||
baseTemplate = try environment.loadTemplate(name: "invalid-base.html")
|
baseTemplate = try environment.loadTemplate(name: "invalid-base.html")
|
||||||
|
|
||||||
try expectError(reason: "filter error",
|
try expectError(reason: "filter error",
|
||||||
childToken: "block.super",
|
childToken: "extends \"invalid-base.html\"",
|
||||||
baseToken: "target|unknown")
|
baseToken: "target|unknown")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,5 +32,24 @@ final class InheritanceTests: XCTestCase {
|
|||||||
Child_Body
|
Child_Body
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
it("can render block.super in if tag") {
|
||||||
|
let template = try self.environment.loadTemplate(name: "if-block-child.html")
|
||||||
|
|
||||||
|
try expect(try template.render(["sort": "new"])) == """
|
||||||
|
Title - Nieuwste spellen
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
try expect(try template.render(["sort": "upcoming"])) == """
|
||||||
|
Title - Binnenkort op de agenda
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
try expect(try template.render(["sort": "near-me"])) == """
|
||||||
|
Title - In mijn buurt
|
||||||
|
|
||||||
|
"""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
Tests/StencilTests/fixtures/if-block-child.html
Normal file
2
Tests/StencilTests/fixtures/if-block-child.html
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
{% extends "if-block.html" %}
|
||||||
|
{% block title %}{% if sort == "new" %}{{ block.super }} - Nieuwste spellen{% elif sort == "upcoming" %}{{ block.super }} - Binnenkort op de agenda{% elif sort == "near-me" %}{{ block.super }} - In mijn buurt{% endif %}{% endblock %}
|
||||||
1
Tests/StencilTests/fixtures/if-block.html
Normal file
1
Tests/StencilTests/fixtures/if-block.html
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{% block title %}Title{% endblock %}
|
||||||
Reference in New Issue
Block a user