4

我可以举一个关于 Liquid 地图过滤器如何工作的通用示例吗?似乎没有这方面的文档。映射过滤器是“在给定属性上映射/收集数组” - 但是您如何确定属性?

此示例存在 -液体模板映射过滤器- 但它似乎特定于单个用例。地图一般如何工作?

我知道要过滤的输出是数组,但是map的参数是什么?

例如:

{{ page.tags | map: 'name' }}

什么名字?它是要输出的值的标签吗?对于给定的对象,如何确定该标签可能是什么?

4

1 回答 1

6

map 通过遍历元素数组的每个元素,然后获取给定属性的值来工作。

您必须知道要传入的元素数组的属性是什么。

即:如果您有一个collection.products,并且您想获取集合中每个产品的句柄,然后以逗号分隔的字符串返回它们,您可以执行以下操作:

{{ collection.products | map: 'handle' | join: ',' }}

这将列出所有产品句柄:

across-the-mountains,alpine-meadow,appproaching-storm,arboretum-poster,around-st-peters,basic-tee,bryce-canyon-sunrise,crocuses,flower-print-mum,gatineau-park-stream,product-2,hooded-sweater,product-3,last-light-at-sandbanks,last-light-on-opabin-plateau,library-of-parliament,library-of-parliament-poster,long-beach-tofino,magnolia,meadow-2,mountain-panoramic-2,ready-to-frame-tulip-print-10,ready-to-frame-tulip-print-11,ready-to-frame-tulip-print-12,ready-to-frame-tulip-print-13,ready-to-frame-tulip-print-14,ready-to-frame-tulip-print-15,ready-to-frame-tulip-print-2,ready-to-frame-tulip-print-3,ready-to-frame-tulip-print-4,ready-to-frame-tulip-print-6,ready-to-frame-tulip-print-7,ready-to-frame-tulip-print-8,ready-to-frame-tulip-print-9,ready-to-frame-tulip-print-5,ready-to-frame-tulip-print-1,sample,shopify-shirt,shirt,sunset-on-lake-ohara,t-shirt,library-of-parliament2,test-prdouct,test-prod,test-product-1,test-product,testing,the-arboretum-poster,the-arboretum-central-experimental-farm,the-colosseum
于 2012-09-06T15:38:43.993 回答