我正在尝试使用Liquid语法从对象列表创建一个数组:
{% for operation in menuItems %}
{% assign words1 = operation.Title | split: '_' %}
{% assign controllerName = words1 | first %}
{% assign controllersTmp = controllersTmp | append: '_' | append: controllerName %}
{% endfor %}
我想拆分controllersTmp
以获取我的数组,但此时我的controllersTmp
为空。
任何帮助?
答案 0 :(得分:19)
您可以使用变通方法concat
直接创建一个新的空数组controllers
和split:''
,将controllerName
转换为数组。结果直接是一个数组,没有额外的字符串操作。
{% assign controllers = '' | split: '' %}
{% for operation in menuItems %}
{% assign controllerName = operation.Title | split: '_' | first | split: '' %}
{% assign controllers = controllers | concat: controllerName %}
{% endfor %}
答案 1 :(得分:2)
什么对我有用
{% assign otherarticles = "" | split: ',' %}
{% assign software_engineering = "" | split: ',' %}
{% for file in site.static_files %}
{% if file.extname == ".html" %}
{% if file.path contains "software_engineering" %}
{% assign software_engineering = software_engineering | push: file %}
{% else %}
{% assign otherarticles = otherarticles | push: file %}
{% endif %}
{% endif %}
{% endfor %}
答案 2 :(得分:0)
你必须初始化你的变量controllersTmp:
{% assign controllersTmp = '' %}