Skip to content
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

Return only unique values in the output of the sort filter #443

Closed
wants to merge 1 commit into from

Conversation

steelman
Copy link

Alternatively a separate uniq filter may be developed.

@untitaker
Copy link
Contributor

  • This should probably document that the items must be hashable.
  • I'm not sure there's a good enough usecase for this. What about {{ set(x)|sort }}?
  • It'd probably make sense to implement this as separate filter.

@steelman
Copy link
Author

Re 1: Indeed, I didn't check the details.

Re 2: I haven't seen this anywhere in the documentation and I am new to Jinja enough not to know which parts of it are common with Python. Too bad it doesn't work.

Traceback (most recent call last):
  File "foo.py", line 526, in <module>
    timeoffs = timeoffs,
  File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "templates/weekly_for_hq.html.j2", line 83, in top-level template code
    {# Staff #}<td>{{ set(engineers)|sort|join(', ') }}</td>
jinja2.exceptions.UndefinedError: 'set' is undefined

Re 3: I thought about it. However, I prefered sorted output anyway so I came up with something simmilar to sort -u.

@untitaker
Copy link
Contributor

Re 2: I'm not so sure anymore if that builtin is actually available in Jinja. I think it's not.
Re 3: On the CLI there's also uniq available.

FWIW you can add your own filters and globals to Jinja, and since Python doesn't have a comparable function, I am not sure if this might not be out of scope -- even if only slightly. You're the first one to request this, so I'd like to just keep this open and see how many people +1 this.

@snoack
Copy link
Contributor

snoack commented Aug 4, 2015

I'd agree that a built-in way to eliminate duplicates from a sequence would be desirable. In fact, I ended up implementing a unique filter myself for a couple of projects, and already asked to have it added to upstream jinja2, 4 years ago as part of #66, see snoack/jinja2@2d9a896. But I think it should be a separate filter for larger flexibility. So one can eliminate duplicates from a sequence while preserving order.

@snoack snoack mentioned this pull request Aug 4, 2015
@snoack
Copy link
Contributor

snoack commented Aug 4, 2015

I have just rebased and improved my old patch, and submitted a new pull request, see #469.

@jeffwidman
Copy link
Contributor

I'm not against adding a filter like unique or set, but like @untitaker I'd rather see it as a separate filter (as implemented in #469) rather than as a param on sort.

@jeffwidman jeffwidman closed this Apr 12, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants