Add support for first/last functions
This commit is contained in:
@@ -1,10 +1,22 @@
|
||||
|
||||
struct HBMustacheContext {
|
||||
let first: Bool
|
||||
let last: Bool
|
||||
struct HBMustacheContext: HBMustacheMethods {
|
||||
var first: Bool
|
||||
var last: Bool
|
||||
|
||||
init(first: Bool = false, last: Bool = false) {
|
||||
self.first = first
|
||||
self.last = last
|
||||
}
|
||||
|
||||
func runMethod(_ name: String) -> Any? {
|
||||
switch name {
|
||||
case "first":
|
||||
return self.first
|
||||
case "last":
|
||||
return self.last
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,9 +7,19 @@ protocol HBMustacheSequence {
|
||||
extension Sequence {
|
||||
func renderSection(with template: HBMustacheTemplate) -> String {
|
||||
var string = ""
|
||||
for obj in self {
|
||||
string += template.render(obj)
|
||||
var context = HBMustacheContext(first: true)
|
||||
var iterator = self.makeIterator()
|
||||
guard var currentObject = iterator.next() else { return "" }
|
||||
|
||||
while let object = iterator.next() {
|
||||
string += template.render(currentObject, context: context)
|
||||
currentObject = object
|
||||
context.first = false
|
||||
}
|
||||
|
||||
context.last = true
|
||||
string += template.render(currentObject, context: context)
|
||||
|
||||
return string
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ extension HBMustacheTemplate {
|
||||
case .text(let text):
|
||||
string += text
|
||||
case .variable(let variable, let method):
|
||||
if let child = getChild(named: variable, from: object, method: method) {
|
||||
if let child = getChild(named: variable, from: object, method: method, context: context) {
|
||||
if let template = child as? HBMustacheTemplate {
|
||||
string += template.render(object)
|
||||
} else {
|
||||
@@ -15,15 +15,15 @@ extension HBMustacheTemplate {
|
||||
}
|
||||
}
|
||||
case .unescapedVariable(let variable, let method):
|
||||
if let child = getChild(named: variable, from: object, method: method) {
|
||||
if let child = getChild(named: variable, from: object, method: method, context: context) {
|
||||
string += String(describing: child)
|
||||
}
|
||||
case .section(let variable, let method, let template):
|
||||
let child = getChild(named: variable, from: object, method: method)
|
||||
let child = getChild(named: variable, from: object, method: method, context: context)
|
||||
string += renderSection(child, parent: object, with: template)
|
||||
|
||||
case .invertedSection(let variable, let method, let template):
|
||||
let child = getChild(named: variable, from: object, method: method)
|
||||
let child = getChild(named: variable, from: object, method: method, context: context)
|
||||
string += renderInvertedSection(child, parent: object, with: template)
|
||||
|
||||
case .partial(let name):
|
||||
@@ -63,7 +63,7 @@ extension HBMustacheTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
func getChild(named name: String, from object: Any, method: String?) -> Any? {
|
||||
func getChild(named name: String, from object: Any, method: String?, context: HBMustacheContext?) -> Any? {
|
||||
func _getChild(named names: ArraySlice<String>, from object: Any) -> Any? {
|
||||
guard let name = names.first else { return object }
|
||||
let childObject: Any?
|
||||
@@ -81,6 +81,8 @@ extension HBMustacheTemplate {
|
||||
let child: Any?
|
||||
if name == "." {
|
||||
child = object
|
||||
} else if name == "", method != nil {
|
||||
child = context
|
||||
} else {
|
||||
let nameSplit = name.split(separator: ".").map { String($0) }
|
||||
child = _getChild(named: nameSplit[...], from: object)
|
||||
|
||||
Reference in New Issue
Block a user