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

@@ -11,7 +11,7 @@ public final class HBMustacheTemplate {
/// - Parameter object: Object to render
/// - Returns: Rendered text
public func render(_ object: Any) -> String {
self.render([object], context: nil)
self.render(context: .init(object))
}
internal init(_ tokens: [Token]) {
@@ -22,8 +22,10 @@ public final class HBMustacheTemplate {
self.library = library
for token in self.tokens {
switch token {
case .section(_, _, let template), .invertedSection(_, _, let template):
case .section(_, _, let template), .invertedSection(_, _, let template), .inheritedSection(_, let template):
template.setLibrary(library)
case .partial(_, _, let templates):
templates?.forEach { $1.setLibrary(library) }
default:
break
}
@@ -36,7 +38,8 @@ public final class HBMustacheTemplate {
case unescapedVariable(name: String, method: String? = nil)
case section(name: String, method: String? = nil, template: HBMustacheTemplate)
case invertedSection(name: String, method: String? = nil, template: HBMustacheTemplate)
case partial(String, indentation: String?)
case inheritedSection(name: String, template: HBMustacheTemplate)
case partial(String, indentation: String?, inherits: [String: HBMustacheTemplate]?)
}
let tokens: [Token]