Template inheritance (#9)

* Move all context variables into HBMustacheContext

* Add support for reading inherited sections

* Render inherited tokens

* Test inheritance spec, fix two minor issues

* fix warning

* swift format
This commit is contained in:
Adam Fowler
2021-03-22 12:02:22 +00:00
committed by GitHub
parent af345e9138
commit 35d52603e2
9 changed files with 232 additions and 59 deletions

View File

@@ -51,4 +51,51 @@ final class PartialTests: XCTestCase {
""")
}
/// test inheritance
func testInheritance() throws {
let library = HBMustacheLibrary()
try library.register(
"""
<head>
<title>{{$title}}Default title{{/title}}</title>
</head>
""",
named: "header"
)
try library.register(
"""
<html>
{{$header}}{{/header}}
{{$content}}{{/content}}
</html>
""",
named: "base"
)
try library.register(
"""
{{<base}}
{{$header}}
{{<header}}
{{$title}}My page title{{/title}}
{{/header}}
{{/header}}
{{$content}}<h1>Hello world</h1>{{/content}}
{{/base}}
""",
named: "mypage"
)
XCTAssertEqual(library.render({}, withTemplate: "mypage")!, """
<html>
<head>
<title>My page title</title>
</head>
<h1>Hello world</h1>
</html>
""")
}
}