2012-09-06 160 views
3

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

这个例子存在 - Liquid Template Map Filter - 但它似乎特定于一个用例。地图如何工作?

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

例如:

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

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

回答

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