feat(filters): Allow filters with arguments

This commit is contained in:
Kyle Fuller
2016-11-27 01:59:57 +00:00
parent 1e3afc0dd5
commit 60b378d482
6 changed files with 95 additions and 10 deletions

View File

@@ -197,7 +197,27 @@ let rendered = try template.render(context, namespace: namespace)
#### Registering custom filters
```swift
namespace.registerFilter("double") { value in
namespace.registerFilter("double") { (value: Any?) in
if let value = value as? Int {
return value * 2
}
return value
}
```
#### Registering custom filters with arguments
```swift
namespace.registerFilter("multiply") { (value: Any?, arguments: [Any?]) in
let amount: Int
if let value = arguments.first as? Int {
amount = value
} else {
throw TemplateSyntaxError("multiple tag must be called with an integer argument")
}
if let value = value as? Int {
return value * 2
}