Add support for dynamic partials
This commit is contained in:
@@ -8,7 +8,11 @@ extension HBMustacheTemplate {
|
||||
string += text
|
||||
case .variable(let variable):
|
||||
if let child = getChild(named: variable, from: object) {
|
||||
string += encodedEscapedCharacters(String(describing: child))
|
||||
if let template = child as? HBMustacheTemplate {
|
||||
string += template.render(object, library: library)
|
||||
} else {
|
||||
string += encodedEscapedCharacters(String(describing: child))
|
||||
}
|
||||
}
|
||||
case .unescapedVariable(let variable):
|
||||
if let child = getChild(named: variable, from: object) {
|
||||
|
||||
@@ -192,6 +192,7 @@ final class TemplateRendererTests: XCTestCase {
|
||||
""")
|
||||
}
|
||||
|
||||
/// Testing partials
|
||||
func testMustacheManualExample9() throws {
|
||||
let library = HBMustacheLibrary()
|
||||
let template = try HBMustacheTemplate(string: """
|
||||
@@ -215,4 +216,28 @@ final class TemplateRendererTests: XCTestCase {
|
||||
|
||||
""")
|
||||
}
|
||||
|
||||
/// Testing dynamic partials
|
||||
func testDynamicPartials() throws {
|
||||
let library = HBMustacheLibrary()
|
||||
let template = try HBMustacheTemplate(string: """
|
||||
<h2>Names</h2>
|
||||
{{partial}}
|
||||
""")
|
||||
let template2 = try HBMustacheTemplate(string: """
|
||||
{{#names}}
|
||||
<strong>{{.}}</strong>
|
||||
{{/names}}
|
||||
""")
|
||||
library.register(template, named: "base")
|
||||
|
||||
let object: [String: Any] = ["names": ["john", "adam", "claire"], "partial": template2]
|
||||
XCTAssertEqual(library.render(object, withTemplateNamed: "base"), """
|
||||
<h2>Names</h2>
|
||||
<strong>john</strong>
|
||||
<strong>adam</strong>
|
||||
<strong>claire</strong>
|
||||
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user