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

Support for "contains" in "where" filter? #2787

Closed
swanson opened this issue Aug 20, 2014 · 6 comments
Closed

Support for "contains" in "where" filter? #2787

swanson opened this issue Aug 20, 2014 · 6 comments

Comments

@swanson
Copy link
Contributor

swanson commented Aug 20, 2014

I'd like to be able to filter posts on my site based on the inclusion of a certain tag. Currently, I have to do it like this:

# Work-around
{% for post in site.posts %}
  {% if post.tags contains "ruby" %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
  {% endif %}
{% endfor %}

I would like support for checking if a collection contains an item as part of the where clause. The work-around is okay, but it will require extra effort if we want to e.g. limit it to the first 5 posts matching a tag (since you'd have to track the count manually).

Maybe something like:

{% for post in site.posts | where_contains: "tags", "ruby" %}
  <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}

or if tags are important enough,

{% for post in site.posts | with_tag: "ruby" %}
  <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}

I'm happy to work on a PR if this is appropriate for Jekyll core - I'm less interested in a custom plugin since all my Jekyll stuff is deployed to GH Pages.

@parkr
Copy link
Member

parkr commented Aug 20, 2014

Does site.tags not satisfy your need?

"tags" => post_attr_hash('tags'),

@swanson
Copy link
Contributor Author

swanson commented Aug 20, 2014

@parkr - ah, had not seen that. That will serve my needs for this case.

@swanson swanson closed this as completed Aug 20, 2014
@parkr
Copy link
Member

parkr commented Aug 20, 2014

Glad to hear we already have what you need!

@chimericdream
Copy link

Sorry to resurrect a relatively old issue, but I ran into this recently as well. In my case, I want to filter a collection based on a specific category. Each collection item can belong to an arbitrary number of categories, and I need to be able to get a list of items in a particular category.

Something like this is what I am hoping for:

{% assign item_list = site.items | contains:"item.categories", "Some Category" %}

@Lemez
Copy link

Lemez commented Oct 11, 2015

Me too.
The tags filter is great, but I would like present my posts to the user with different categories of tag:
location-tags, artist-tags, content-tags etc.

So:
{% assign pages_list = (site.posts | where: "location" contains "London") %}

would be ideal.

@envygeeks
Copy link
Contributor

We are not SQL, so your example will never happen @Lemez but if you would like to suggest a more viable syntax that doesn't require us to parse please file a new ticket and spark a conversation rather than necromancing an old ticket. Thanks.

@jekyll jekyll locked and limited conversation to collaborators Oct 11, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants