Skip to content

Commit

Permalink
always evaluate block.super even if it is not used
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka authored and djbe committed Jul 27, 2022
1 parent a6d0428 commit 701221c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 33 deletions.
37 changes: 5 additions & 32 deletions Sources/Inheritence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion Tests/StencilTests/EnvironmentSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/StencilTests/InheritanceSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
}
}
}
2 changes: 2 additions & 0 deletions Tests/StencilTests/fixtures/if-block-child.html
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions Tests/StencilTests/fixtures/if-block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% block title %}Title{% endblock %}

0 comments on commit 701221c

Please sign in to comment.