diff --git a/Sources/Inheritence.swift b/Sources/Inheritence.swift index 87e58964..33950fdd 100644 --- a/Sources/Inheritence.swift +++ b/Sources/Inheritence.swift @@ -136,7 +136,11 @@ class BlockNode: NodeType { func render(_ context: Context) throws -> String { 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 do { return try context.push(dictionary: childContext) { @@ -149,35 +153,4 @@ class BlockNode: NodeType { 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 - } } diff --git a/Tests/StencilTests/EnvironmentSpec.swift b/Tests/StencilTests/EnvironmentSpec.swift index f077ef2a..7d5ad961 100644 --- a/Tests/StencilTests/EnvironmentSpec.swift +++ b/Tests/StencilTests/EnvironmentSpec.swift @@ -320,7 +320,7 @@ final class EnvironmentBaseAndChildTemplateTests: XCTestCase { baseTemplate = try environment.loadTemplate(name: "invalid-base.html") try expectError(reason: "filter error", - childToken: "block.super", + childToken: "extends \"invalid-base.html\"", baseToken: "target|unknown") } diff --git a/Tests/StencilTests/InheritanceSpec.swift b/Tests/StencilTests/InheritanceSpec.swift index 4f91affd..60f4fffb 100644 --- a/Tests/StencilTests/InheritanceSpec.swift +++ b/Tests/StencilTests/InheritanceSpec.swift @@ -32,5 +32,24 @@ final class InheritanceTests: XCTestCase { 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 + + """ + } } } diff --git a/Tests/StencilTests/fixtures/if-block-child.html b/Tests/StencilTests/fixtures/if-block-child.html new file mode 100644 index 00000000..8a76eea6 --- /dev/null +++ b/Tests/StencilTests/fixtures/if-block-child.html @@ -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 %} diff --git a/Tests/StencilTests/fixtures/if-block.html b/Tests/StencilTests/fixtures/if-block.html new file mode 100644 index 00000000..b937e4d5 --- /dev/null +++ b/Tests/StencilTests/fixtures/if-block.html @@ -0,0 +1 @@ +{% block title %}Title{% endblock %}