Search code examples
phptwig-extension

Key inside Array won't parse correctly in TWIG


testplugin.testplugin.firstkey has following value 1, 2, 3

I have follwing code written in TWIG:

{% set key1 = [config("testplugin.testplugin.firstkey")] %}

{% for ids in key1 %}
    {% set key1 = ids %}
    GO-{{ ids }} {% if not loop.last %},{% endif %}
{% endfor %}

The problem is, that the config("testplugin.testplugin.firstkey") won't get parsed correctly. Actually it only gets parsed as one value instead of 3 seperat values in an array. But when i define the values manually - without a variable - it works as it should:

{% set key2 = [1, 2, 3] %}

{% for ids in key2 %}
    {% set key2 = ids %}
    GO-{{ ids }} {% if not loop.last %},{% endif %}
{% endfor %}

The first code does this:

GO-1, 2, 3

the second one looks like this (as it should):

GO-1, GO-2, GO-3

So my question is, why doeas the first code won't work properly?


Solution

  • I could figure it out by myself:

    {% set key1 = config("testplugin.testplugin.firstkey"))|split(',') %}
    
    {% for ids in key1 %}
        {% set key1 = ids %}
        GO-{{ ids }} {% if not loop.last %},{% endif %}
    {% endfor %}
    

    Thanks anyway ;-)