我有一个名为行业的字符串字段,人们将在其中输入他们的行业。例如农业、制造业、IT、景观美化等。
我想使用liquid输出此数据,但是,我想将其放入下拉菜单中的Web应用程序搜索表单中,以便用户可以搜索特定的字段。因此,我不想添加任何重复的项目。
例如,来自用户的条目包括:农业,制造业,农业,IT,景观,农业-你可以看到农业被使用了3次。如果我使用下面的,它将被列出3次:
<select>
{module_webapps id="12345" collection="industry" filter="all" template=""}
{% for item in industry.items %}
<option value="{{item.industry}}">{{item.industry}}</option>
{% endfor %}
</select>
如何使用循环或数组来仅显示行业一次,并隐藏所有其他重复项?
谢谢
发布于 2016-12-20 14:01:51
您可以对所有项目的字符串执行capture
操作。然后使用字符串过滤器split
将其转换为基于分隔符的数组。然后使用uniq
数组筛选器删除所有重复项。最后,迭代结果数组以构建下拉菜单。
<select>
{module_webapps id="12345" collection="industry" filter="all" template=""}
{% capture items %}
{% for item in industry.items %}
{{item.industry}},
{% endfor %}
{% endcapture %}
{% for item in items | split: ',' | uniq %}
<option value="{{item}}">{{item}}</option>
{% endfor %}
</select>
https://stackoverflow.com/questions/41234319
复制相似问题