Shopify / liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.
https://shopify.github.io/liquid/
MIT License
11.05k stars 1.38k forks source link

Deeper search in where array filter #1388

Open thomascoppein opened 3 years ago

thomascoppein commented 3 years ago

Hello, is it possible to dive deeper in a where filter?

For example (this is assigned inside a loop over the blocks): {% assign filtered_array = main_array | where: 'settings.category', 'category-1' %}

Thanks

parsaydin commented 3 years ago

Hi @thomascoppein ,

I don't think it's possible to use more than 2 values for 'where' filter. I also reviewed the official doc and it says so. Please check this out: https://shopify.github.io/liquid/filters/where/

I was thinking about this question, I thought that maybe you can use 'map' filter before 'where'. For example: {% assign filtered_array = main_array | map: 'sample-category' | where: 'settings.category', 'category-1' %}

Let me know if the answer is helpful or not.

liamgriffin commented 1 year ago

Additionally, it would be beneficial to expand the where filter so that non-string properties can be passed to it. For example, this will not output a list of available products:

{% assign available_products = collection.products | where: "available", "true" %}

Products we have:

{% for product in available_products -%}
  - {{ product.title }}
{%- endfor %}

because product.available outputs a boolean. Expanding which properties are accepted will also unlock new opportunities for using metafields with the where filter.

dylanahsmith commented 1 year ago

Additionally, it would be beneficial to expand the where filter so that non-string properties can be passed to it.

Can't you use where: "available", true? It doesn't look like there is any coercion of the target_value argument. It looks like it is just an explicit nil that isn't supported (e.g. where: "optional_property", nil would behave like where: "optional_property", doing the opposite of the intended filter)