Cache rendered blocks content to reuse them in further calls
This commit is contained in:
committed by
David Jennes
parent
4d3f911f5d
commit
8c379296ca
@@ -85,4 +85,19 @@ public class Context {
|
|||||||
|
|
||||||
return accumulator
|
return accumulator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Cache result of block by its name in the context top-level, so that it can be later rendered
|
||||||
|
/// via `{{ block.name }}`
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - name: The name of the stored block
|
||||||
|
/// - content: The block's rendered content
|
||||||
|
public func cacheBlock(_ name: String, content: String) {
|
||||||
|
if var block = dictionaries.first?["block"] as? [String: String] {
|
||||||
|
block[name] = content
|
||||||
|
dictionaries[0]["block"] = block
|
||||||
|
} else {
|
||||||
|
dictionaries.insert(["block": [name: content]], at: 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -151,6 +151,8 @@ class BlockNode: NodeType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return try renderNodes(nodes, context)
|
let result = try renderNodes(nodes, context)
|
||||||
|
context.cacheBlock(name, content: result)
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,4 +52,22 @@ final class InheritanceTests: XCTestCase {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testInheritanceCache() {
|
||||||
|
it("can call block twice") {
|
||||||
|
let template: Template = "{% block repeat %}Block{% endblock %}{{ block.repeat }}"
|
||||||
|
try expect(try template.render()) == "BlockBlock"
|
||||||
|
}
|
||||||
|
|
||||||
|
it("renders child content when calling block twice in base template") {
|
||||||
|
let template = try self.environment.loadTemplate(name: "child-repeat.html")
|
||||||
|
try expect(try template.render()) == """
|
||||||
|
Super_Header Child_Header
|
||||||
|
Child_Body
|
||||||
|
Repeat
|
||||||
|
Super_Header Child_Header
|
||||||
|
Child_Body
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
Tests/StencilTests/fixtures/base-repeat.html
Normal file
5
Tests/StencilTests/fixtures/base-repeat.html
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{% block header %}Header{% endblock %}
|
||||||
|
{% block body %}Body{% endblock %}
|
||||||
|
Repeat
|
||||||
|
{{ block.header }}
|
||||||
|
{{ block.body }}
|
||||||
3
Tests/StencilTests/fixtures/child-repeat.html
Normal file
3
Tests/StencilTests/fixtures/child-repeat.html
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{% extends "base-repeat.html" %}
|
||||||
|
{% block header %}Super_{{ block.super }} Child_Header{% endblock %}
|
||||||
|
{% block body %}Child_Body{% endblock %}
|
||||||
Reference in New Issue
Block a user