Add methods for Array and Dictionary
This commit is contained in:
@@ -7,29 +7,23 @@ extension HBMustacheTemplate {
|
||||
case .text(let text):
|
||||
string += text
|
||||
case .variable(let variable, let method):
|
||||
if var child = getChild(named: variable, from: object) {
|
||||
if let method = method,
|
||||
let runnable = child as? HBMustacheBaseMethods {
|
||||
if let result = runnable.runMethod(method) {
|
||||
child = result
|
||||
}
|
||||
}
|
||||
if let child = getChild(named: variable, from: object, method: method) {
|
||||
if let template = child as? HBMustacheTemplate {
|
||||
string += template.render(object, library: library)
|
||||
} else {
|
||||
string += htmlEscape(String(describing: child))
|
||||
}
|
||||
}
|
||||
case .unescapedVariable(let variable):
|
||||
if let child = getChild(named: variable, from: object) {
|
||||
case .unescapedVariable(let variable, let method):
|
||||
if let child = getChild(named: variable, from: object, method: method) {
|
||||
string += String(describing: child)
|
||||
}
|
||||
case .section(let variable, let template):
|
||||
let child = getChild(named: variable, from: object)
|
||||
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)
|
||||
|
||||
case .invertedSection(let variable, let template):
|
||||
let child = getChild(named: variable, from: object)
|
||||
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)
|
||||
|
||||
case .partial(let name):
|
||||
@@ -67,7 +61,7 @@ extension HBMustacheTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
func getChild(named name: String, from object: Any) -> Any? {
|
||||
func getChild(named name: String, from object: Any, method: String?) -> Any? {
|
||||
func _getChild(named names: ArraySlice<String>, from object: Any) -> Any? {
|
||||
guard let name = names.first else { return object }
|
||||
let childObject: Any?
|
||||
@@ -82,9 +76,20 @@ extension HBMustacheTemplate {
|
||||
return _getChild(named: names2, from: childObject!)
|
||||
}
|
||||
|
||||
if name == "." { return object }
|
||||
let nameSplit = name.split(separator: ".").map { String($0) }
|
||||
return _getChild(named: nameSplit[...], from: object)
|
||||
let child: Any?
|
||||
if name == "." {
|
||||
child = object
|
||||
} else {
|
||||
let nameSplit = name.split(separator: ".").map { String($0) }
|
||||
child = _getChild(named: nameSplit[...], from: object)
|
||||
}
|
||||
if let method = method,
|
||||
let runnable = child as? HBMustacheBaseMethods {
|
||||
if let result = runnable.runMethod(method) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
return child
|
||||
}
|
||||
|
||||
private static let htmlEscapedCharacters: [Character: String] = [
|
||||
@@ -140,3 +145,37 @@ extension Array: HBSequence {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
extension ReversedCollection: HBSequence {
|
||||
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
|
||||
var string = ""
|
||||
for obj in self {
|
||||
string += template.render(obj, library: library)
|
||||
}
|
||||
return string
|
||||
}
|
||||
|
||||
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
|
||||
if count == 0 {
|
||||
return template.render(self, library: library)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
extension EnumeratedSequence: HBSequence {
|
||||
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
|
||||
var string = ""
|
||||
for obj in self {
|
||||
string += template.render(obj, library: library)
|
||||
}
|
||||
return string
|
||||
}
|
||||
|
||||
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
|
||||
if self.underestimatedCount == 0 {
|
||||
return template.render(self, library: library)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user