user2818060
user2818060

Reputation: 845

How To convert string to Array in Twig

what i Need:

string as follows:

  ["Product_Name"]=> string(362) "Top End Transport System,Band Combiner Devices,Our Mid Level Transport System,The Introductory Transport System,In-Line Amplification Systems,Intelligent Ethernet Access System,Ethernet Access System,Intelligent Ethernet Access System (Ieas 05),Intelligent Ethernet Access System (Ieas 06),Intelligent Ethernet Access System (Ieas 03),Ethernet Aggregation Device" }

here is twig code:

            {% set foo = item.Product_Name|split(',') %}
            {{ dump (foo) }}
           {% for i in item.Product_Name|slice(0, 5) %}
            {{ dump(i) }}
            {% endfor %}

Upvotes: 4

Views: 17737

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

You have to loop over your foo variable in order to print your products slice foo in order to get first five names foo|slice(0, 5)

{% set foo = item.Product_Name|split(',') %}
{% for i in foo|slice(0, 5) %}
   {{ dump(i) }}
{% endfor %}

Upvotes: 3

Related Questions