feat(default filter): Support multiple defaults

This commit is contained in:
Kyle Fuller
2016-11-28 19:10:48 +00:00
parent 679344f53b
commit b1da85b140
2 changed files with 17 additions and 1 deletions

View File

@@ -33,7 +33,17 @@ func lowercase(_ value: Any?) -> Any? {
} }
func defaultFilter(value: Any?, arguments: [Any?]) -> Any? { func defaultFilter(value: Any?, arguments: [Any?]) -> Any? {
return value ?? arguments.first as Any if let value = value {
return value
}
for argument in arguments {
if let argument = argument {
return argument
}
}
return nil
} }
func joinFilter(value: Any?, arguments: [Any?]) throws -> Any? { func joinFilter(value: Any?, arguments: [Any?]) throws -> Any? {

View File

@@ -101,6 +101,12 @@ func testFilter() {
let result = try template.render(Context(dictionary: [:])) let result = try template.render(Context(dictionary: [:]))
try expect(result) == "Hello World" try expect(result) == "Hello World"
} }
$0.it("supports multiple defaults") {
let template = Template(templateString: "Hello {{ name|default:a,b,c,\"World\" }}")
let result = try template.render(Context(dictionary: [:]))
try expect(result) == "Hello World"
}
} }
describe("join filter") { describe("join filter") {