liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Adding a new "where" filters

Open bakura10 opened this issue 3 years ago • 4 comments

Hi,

The where filter is very useful, but I would suggest having a few improvements:

where_not

This filter would do the opposite of where filter:

{% assign blocks_to_render = section.blocks | where_not: 'type', 'divider' %}

Allow dot notation

With metafields and dynamic connection, it is increasingly common that merchant create a section with blocks, and bound the content with metafields.

For instance, let's imagine a "testimonials" section where the merchant would create 5 blocks. Some products may have 5, 4, 3, 2 or even no active blocks, depending on the product. This currently makes rendering very cumbersome. I would love having a dot notation. For instance:

{% assign rendered_blocks = section.blocks | where: 'settings.testimonial' %}

{% assign rendered_blocks = section.blocks | where: 'settings.product' %}

The idea here is to return actual block instances, but that would be filtered. This would allow to generate section code based on the actual content indicated by the merchant.

bakura10 avatar Mar 22 '22 11:03 bakura10

A PR for dot notation in where filter is open here https://github.com/Shopify/liquid/pull/1436

andershagbard avatar Apr 23 '22 08:04 andershagbard

I'd recommend calling the inverse of where reject, as that maps to the identical feature in Ruby.

henryiii avatar May 04 '22 20:05 henryiii

Is there any update on this? I know it's been 1 year without a reply, but it's still relevant and would be an insane help to have on Shopify :)

FalconPilot avatar May 05 '23 14:05 FalconPilot

where_not is really required! There is no way to filter for nested properties that are not empty!

This filters section blocks where row_content is empty: {% assign blocks = section.blocks | map: 'settings' | where: 'row_content', empty %}

There is absolutely no way to filter blocks where row_content is not empty!

MLDMoritz avatar Jul 17 '23 13:07 MLDMoritz