Add index filter

This commit is contained in:
Adam Fowler
2021-03-14 17:53:20 +00:00
parent 33b488fe40
commit 465ad49f1f
4 changed files with 31 additions and 0 deletions

View File

@@ -2,10 +2,12 @@
struct HBMustacheContext: HBMustacheMethods {
var first: Bool
var last: Bool
var index: Int
init(first: Bool = false, last: Bool = false) {
self.first = first
self.last = last
self.index = 0
}
func runMethod(_ name: String) -> Any? {
@@ -14,6 +16,8 @@ struct HBMustacheContext: HBMustacheMethods {
return self.first
case "last":
return self.last
case "index":
return self.index
default:
return nil
}

View File

@@ -39,3 +39,14 @@ extension Dictionary: HBMustacheMethods {
}
}
}
extension Int: HBMustacheMethods {
func runMethod(_ name: String) -> Any? {
switch name {
case "plus1":
return self + 1
default:
return nil
}
}
}

View File

@@ -15,6 +15,7 @@ extension Sequence {
string += template.render(currentObject, context: context)
currentObject = object
context.first = false
context.index += 1
}
context.last = true