如何在Liquid中的for循环中创建数组? [英] How to create an array in a for loop in Liquid?

查看:462
本文介绍了如何在Liquid中的for循环中创建数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Liquid语法从对象列表中创建一个数组:

I'm trying to create an array from a list of objects using Liquid syntax:

{% for operation in menuItems %}
      {% assign words1 = operation.Title | split: '_' %}
      {% assign controllerName = words1 | first %}
      {% assign controllersTmp = controllersTmp | append: '_' | append: controllerName %}
{% endfor %}

我想分割controllersTmp以获得数组,但是此时我的controllersTmp为空.

I want to split the controllersTmp to get my array, but at this point my controllersTmp is empty.

有帮助吗?

推荐答案

您可以直接创建一个新的空数组controllers

You can directly create a new empty array controllers and concat to it your controllerName converted into an array using the workaround split:''. The result is directly an array, without the extra string manipulations.

{% assign controllers = '' | split: '' %}
{% for operation in menuItems %}
    {% assign controllerName = operation.Title | split: '_' | first | split: '' %}
    {% assign controllers = controllers | concat: controllerName %}
{% endfor %}

这篇关于如何在Liquid中的for循环中创建数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆