Add support for first/last functions

This commit is contained in:
Adam Fowler
2021-03-14 12:47:00 +00:00
parent 5f49dd6953
commit 33b488fe40
5 changed files with 50 additions and 10 deletions

View File

@@ -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
}