Add find, find_index, has, and reject filters to arrays
#1864
Closed
benjaminsehl
started this conversation in
RFCs
Replies: 1 comment
-
|
Closing the discussion, original issue has context and feedback from the community: #1849 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Originally posted as Issue #1849
Author: Shopify
Expected end date: December 5, 2024
Background
Handling arrays in Liquid is challenging.
Developers often need to iterate over arrays to find items or check for their presence. This pattern results in imperative code and verbose templates, which detract from the simplicity and declarative nature that Liquid aims to promote.
{%- liquid assign has_app_block = false for block in section.blocks if block.type == "@app" assign has_app_block = true endif endfor -%} {{ has_app_block }}⏭️ Upcoming: In a different proposal, we'll tackle improvements to the
wherefilter and its limitation of only filtering based on equality. That's so crucial, we aim to handle that separately.Proposal
We should introduce new filters to make it easier to perform common tasks such as extracting or checking the presence of an item in an array. This will make Liquid templates simpler, less verbose, and more declarative.
All filters proposed here do not mutate the source object and return a new instance as a result of the operation:
1.
findandfind_indexThe
findandfind_indexfilters return, respectively, the item and the index of the item with the specified property value; otherwise, they returnnil.{%- liquid assign index = 0 for section_block in section.blocks assign index = index | plus: 1 if block.id == section_block.id break endif endfor %}{{ section.blocks | find: "id", block.id }} {{ section.blocks | find_index: "id", block.id }}2.
hasReturns
truewhen the array includes an item with the specified property value; otherwise, returnsfalse.{%- liquid assign has_app_block = false for block in section.blocks if block.type == "@app" assign has_app_block = true endif endfor -%} {{ has_app_block }}Source
{{ section.blocks | has: "type", "@app" }}3.
rejectWorks almost like the where filter, but returns an array excluding items with the specified property value.
{% assign property_value = false %} {% if something != this %} {% assign property_value = true %} {% endif %} {{ array | where: property_name, property_value }}Source
{{ section.blocks | reject: "type", "@app" }}Call for suggestions
We welcome any feedback or opinions on this proposal. Please share your thoughts by December 5, 2024. Your input is valuable as we prepare to begin active development on this initiative.
Beta Was this translation helpful? Give feedback.
All reactions