-
Notifications
You must be signed in to change notification settings - Fork 223
Added valueForKey filter #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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") | ||
| } | ||
|
|
||
| let key = stringify(arguments[0]) | ||
| return try context.push(dictionary: ["filter_value": value as Any]) { | ||
| return try Variable("filter_value.\(key)").resolve(context) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 This would allow chaining {{ users|valueForKey:"name"|join }} |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
| } | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" }}") |
||
|
|
||
| } | ||
There was a problem hiding this comment.
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)