Files
swiftpm-mustache/Sources/HummingbirdMustache/Sequence.swift
2021-03-15 10:57:37 +00:00

39 lines
1.1 KiB
Swift

protocol HBMustacheSequence {
func renderSection(with template: HBMustacheTemplate) -> String
func renderInvertedSection(with template: HBMustacheTemplate) -> String
}
extension Sequence {
func renderSection(with template: HBMustacheTemplate) -> String {
var string = ""
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.index += 1
}
context.last = true
string += template.render(currentObject, context: context)
return string
}
func renderInvertedSection(with template: HBMustacheTemplate) -> String {
var iterator = makeIterator()
if iterator.next() == nil {
return template.render(self)
}
return ""
}
}
extension Array: HBMustacheSequence {}
extension ReversedCollection: HBMustacheSequence {}