Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an array in a for loop in 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 %}

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

Any help ?

like image 439
Maroine Abdellah Avatar asked Dec 21 '16 13:12

Maroine Abdellah


2 Answers

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 %}
like image 194
jrbedard Avatar answered Nov 12 '22 17:11

jrbedard


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

like image 29
Alex Punnen Avatar answered Nov 12 '22 18:11

Alex Punnen