Add support for proper lambdas (#48)

* Add support for proper lambdas

* Get rid of recursion

Remove renderSectionLambda as I can use renderUnescapedLambda for that.
This commit is contained in:
Adam Fowler
2024-09-19 17:17:50 +01:00
committed by GitHub
parent 8fba85e28c
commit 933fa3d60f
9 changed files with 249 additions and 44 deletions

View File

@@ -34,7 +34,7 @@
///
public struct MustacheLambda {
/// lambda callback
public typealias Callback = (Any, MustacheTemplate) -> String
public typealias Callback = (String) -> Any?
let callback: Callback
@@ -44,7 +44,13 @@ public struct MustacheLambda {
self.callback = cb
}
internal func run(_ object: Any, _ template: MustacheTemplate) -> String {
return self.callback(object, template)
/// Initialize `MustacheLambda`
/// - Parameter cb: function to be called by lambda
public init(_ cb: @escaping () -> Any?) {
self.callback = { _ in cb() }
}
internal func callAsFunction(_ s: String) -> Any? {
return self.callback(s)
}
}