24

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 %}

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

Any help ?

SiHa
  • 7,830
  • 13
  • 34
  • 43
Maroine Abdellah
  • 576
  • 1
  • 5
  • 21

3 Answers3

38

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 %}
jrbedard
  • 3,662
  • 5
  • 30
  • 34
  • Maroine, was this working in API Management template as requested? I tried to do a similar thing but it looks like `concat` cannot be used – Nicolas R Jan 08 '18 at 14:44
  • 1
    If controllerName isn't an array itself (which I don't think it would be in the OP's question), you'd need to use append in the last assign. And you could skip the split in the first assign. – relizt Sep 01 '20 at 21:17
6

What worked for me

{% 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 %}

Alex Punnen
  • 5,287
  • 3
  • 59
  • 71
-3

you have to init your variable controllersTmp :

 {% assign controllersTmp = '' %}
Maroine Abdellah
  • 576
  • 1
  • 5
  • 21