Keep pointer to Library in template

This commit is contained in:
Adam Fowler
2021-03-14 08:51:16 +00:00
parent aaf285154d
commit 06251739d7
6 changed files with 56 additions and 34 deletions

View File

@@ -1,2 +1,2 @@
public typealias HBMustacheLambda = (Any, HBMustacheTemplate, HBMustacheLibrary?) -> String
public typealias HBMustacheLambda = (Any, HBMustacheTemplate) -> String

View File

@@ -10,6 +10,7 @@ public class HBMustacheLibrary {
}
public func register(_ template: HBMustacheTemplate, named name: String) {
template.setLibrary(self)
templates[name] = template
}
@@ -19,7 +20,7 @@ public class HBMustacheLibrary {
public func render(_ object: Any, withTemplateNamed name: String) -> String? {
guard let template = templates[name] else { return nil }
return template.render(object, library: self)
return template.render(object)
}
private var templates: [String: HBMustacheTemplate]

View File

@@ -1,6 +1,6 @@
extension HBMustacheTemplate {
func render(_ object: Any, library: HBMustacheLibrary? = nil, context: HBMustacheContext? = nil) -> String {
func render(_ object: Any, context: HBMustacheContext? = nil) -> String {
var string = ""
for token in tokens {
switch token {
@@ -9,7 +9,7 @@ extension HBMustacheTemplate {
case .variable(let variable, let method):
if let child = getChild(named: variable, from: object, method: method) {
if let template = child as? HBMustacheTemplate {
string += template.render(object, library: library)
string += template.render(object)
} else {
string += htmlEscape(String(describing: child))
}
@@ -20,11 +20,11 @@ extension HBMustacheTemplate {
}
case .section(let variable, let method, let template):
let child = getChild(named: variable, from: object, method: method)
string += renderSection(child, parent: object, with: template, library: library)
string += renderSection(child, parent: object, with: template)
case .invertedSection(let variable, let method, let template):
let child = getChild(named: variable, from: object, method: method)
string += renderInvertedSection(child, parent: object, with: template, library: library)
string += renderInvertedSection(child, parent: object, with: template)
case .partial(let name):
if let text = library?.render(object, withTemplateNamed: name) {
@@ -35,31 +35,31 @@ extension HBMustacheTemplate {
return string
}
func renderSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
func renderSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate) -> String {
switch child {
case let array as HBSequence:
return array.renderSection(with: template, library: library)
return array.renderSection(with: template)
case let bool as Bool:
return bool ? template.render(parent, library: library) : ""
return bool ? template.render(parent) : ""
case let lambda as HBMustacheLambda:
return lambda(parent, template, library)
return lambda(parent, template)
case .some(let value):
return template.render(value, library: library)
return template.render(value)
case .none:
return ""
}
}
func renderInvertedSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
func renderInvertedSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate) -> String {
switch child {
case let array as HBSequence:
return array.renderInvertedSection(with: template, library: library)
return array.renderInvertedSection(with: template)
case let bool as Bool:
return bool ? "" : template.render(parent, library: library)
return bool ? "" : template.render(parent)
case .some:
return ""
case .none:
return template.render(parent, library: library)
return template.render(parent)
}
}
@@ -127,56 +127,56 @@ extension Dictionary: HBMustacheParent where Key == String {
}
protocol HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String
func renderSection(with template: HBMustacheTemplate) -> String
func renderInvertedSection(with template: HBMustacheTemplate) -> String
}
extension Array: HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
func renderSection(with template: HBMustacheTemplate) -> String {
var string = ""
for obj in self {
string += template.render(obj, library: library)
string += template.render(obj)
}
return string
}
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
func renderInvertedSection(with template: HBMustacheTemplate) -> String {
if count == 0 {
return template.render(self, library: library)
return template.render(self)
}
return ""
}
}
extension ReversedCollection: HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
func renderSection(with template: HBMustacheTemplate) -> String {
var string = ""
for obj in self {
string += template.render(obj, library: library)
string += template.render(obj)
}
return string
}
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
func renderInvertedSection(with template: HBMustacheTemplate) -> String {
if count == 0 {
return template.render(self, library: library)
return template.render(self)
}
return ""
}
}
extension EnumeratedSequence: HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
func renderSection(with template: HBMustacheTemplate) -> String {
var string = ""
for obj in self {
string += template.render(obj, library: library)
string += template.render(obj)
}
return string
}
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
func renderInvertedSection(with template: HBMustacheTemplate) -> String {
if self.underestimatedCount == 0 {
return template.render(self, library: library)
return template.render(self)
}
return ""
}

View File

@@ -8,8 +8,20 @@ public class HBMustacheTemplate {
self.tokens = tokens
}
public func render(_ object: Any, library: HBMustacheLibrary? = nil) -> String {
self.render(object, library: library, context: nil)
public func render(_ object: Any) -> String {
self.render(object, context: nil)
}
internal func setLibrary(_ library: HBMustacheLibrary) {
self.library = library
for token in tokens {
switch token {
case .section(_, _, let template), .invertedSection(_, _, let template):
template.setLibrary(library)
default:
break
}
}
}
enum Token {
@@ -22,5 +34,6 @@ public class HBMustacheTemplate {
}
let tokens: [Token]
var library: HBMustacheLibrary?
}