Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- The `{% for %}` tag can now iterate over tuples, structures and classes via
their stored properties.
- Added `split` filter
- Added `valueForKey` filter

### Bug Fixes

Expand Down
14 changes: 9 additions & 5 deletions Sources/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ open class Extension {

/// Registers a template filter with the given name
public func registerFilter(_ name: String, filter: @escaping (Any?, [Any?]) throws -> Any?) {
filters[name] = .arguments({ value, args, _ in try filter(value, args) })
}

public func registerFilter(_ name: String, filter: @escaping (Any?, [Any?], Context) throws -> Any?) {
filters[name] = .arguments(filter)
}
}
Expand Down Expand Up @@ -58,28 +62,28 @@ class DefaultExtension: Extension {
registerFilter("lowercase", filter: lowercase)
registerFilter("join", filter: joinFilter)
registerFilter("split", filter: splitFilter)
registerFilter("valueForKey", filter: valueForKeyFilter)
}
}


protocol FilterType {
func invoke(value: Any?, arguments: [Any?]) throws -> Any?
func invoke(value: Any?, arguments: [Any?], context: Context) throws -> Any?
}

enum Filter: FilterType {
case simple(((Any?) throws -> Any?))
case arguments(((Any?, [Any?]) throws -> Any?))
case arguments(((Any?, [Any?], Context) throws -> Any?))

func invoke(value: Any?, arguments: [Any?]) throws -> Any? {
func invoke(value: Any?, arguments: [Any?], context: Context) throws -> Any? {
switch self {
case let .simple(filter):
if !arguments.isEmpty {
throw TemplateSyntaxError("cannot invoke filter with an argument")
}

return try filter(value)
case let .arguments(filter):
return try filter(value, arguments)
return try filter(value, arguments, context)
}
}
}
11 changes: 11 additions & 0 deletions Sources/Filters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@ func splitFilter(value: Any?, arguments: [Any?]) throws -> Any? {

return value
}

func valueForKeyFilter(value: Any?, arguments: [Any?], context: Context) throws -> Any? {
guard arguments.count == 1 else {
throw TemplateSyntaxError("'split' filter takes a single argument")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like copy/paste error with filter name (split -> valueForKey)

}

let key = stringify(arguments[0])
return try context.push(dictionary: ["filter_value": value as Any]) {
return try Variable("filter_value.\(key)").resolve(context)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense that this could handle arrays, similar to NSArray valueForKey:.

This would allow chaining valueForKey with join.

{{ users|valueForKey:"name"|join }}

}
}
2 changes: 1 addition & 1 deletion Sources/Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FilterExpression : Resolvable {

return try filters.reduce(result) { x, y in
let arguments = try y.1.map { try $0.resolve(context) }
return try y.0.invoke(value: x, arguments: arguments)
return try y.0.invoke(value: x, arguments: arguments, context: context)
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions Tests/StencilTests/FilterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,18 @@ func testFilter() {
}
}

describe("valueForKey filter") {
$0.it("can get value for predefined key") {
let template = Template(templateString: "{{ value|valueForKey:\"name\" }}")
let result = try template.render(Context(dictionary: ["value": ["name": "One"]]))
try expect(result) == "One"
}

$0.it("can get value for runtime key") {
let template = Template(templateString: "{{ value|valueForKey:key }}")
let result = try template.render(Context(dictionary: ["key": "name", "value": ["name": "One"]]))
try expect(result) == "One"
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be a good idea to include a more complex example which validates that the filter uses the logic for variable lookup (perhaps in future some refactoring could break this currently supported behaviour):

let context: [String: Any] = [
    "user": ["name": "Kyle"]
]
let template = Template(templateString: "{{ value|valueForKey:"user.name" }}")


}