Liquid Markup Map Filter Example

我是研究僧i 提交于 2019-12-12 12:09:53

问题


May I have a generic example of how the Liquid map filter works? There doesn't appear to be documentation for this. The map filter is to "map/collect an array on a given property" - but how do you determine the property?

This example exists - Liquid Template Map Filter - but it seems specific to a single use case. How does map work generally?

I understand that the output to be filtered is array, but what is the parameter for map?

For example:

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

What is name? Is it the label for the value to be output? For a given object, how does one determine what that label might be?


回答1:


map works by iterating through each element of the array of elements, and then getting the value for the given property.

You would have to know what the property is for the array of elements you're passing in.

ie: if you had a collection.products, and you want to grab the handle for each product in the collection, and then return them in a comma seperated string, you could then do:

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

and that would list out all the product handles:

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


来源:https://stackoverflow.com/questions/12303149/liquid-markup-map-filter-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!