-1

I am working on a twig form view. I am feeding the view a nested array of elements that will be used to prepopulate the form inputs. I know the array exists on the view since I can show it by using {{ }} or {% %} type statements.

I loop through the array and start creating form elements, but I'm having trouble accessing the key/value pair in the array while in the loop.

Here is my code:

{% for key, value in csvRowData %}
    <tr class='inforow' id='{{formRowId}}'>
        {% for slKey,slValue in startList %}
            {% set inputName = slValue.name %}
            {% set gman1 = slValue.name %}
            {% set inputVal = value.gman1 %}

            <td class="{{slValue.td_class}} ">
                {{inputName}}
                {{inputVal}}

                <div class="form-group">
                {% if slValue.type is same as('text') %}
                    <input type='text' value="{{value.ward}}" data-type="{{slValue.name}}" name="0[{{slValue.name}}]" class="{{slValue.inpt_class}}" placeholder=". {{slValue.placeholder}}">
                {% endif %}
                </div>
            </td>
        {% endfor %}
    </tr>
{% endfor %}

Here is the csvRowData array so you can see it's structure etc:

Array
(
[0] => Array
    (
        [ward] => AWARD
        [mrn] => 123456
        [client_name] => gman donster
        [bill_number] => bill1
        [salesforce_id] => sf1
        [michart_results] => on
        [dd_test] => Han
    )

[1] => Array
    (
        [ward] => BWARD
        [mrn] => 789012
        [client_name] => logan thunder
        [bill_number] => bill2
        [salesforce_id] => sf2
        [michart_results] => Greedo
    )

[2] => Array
    (
        [ward] => CWARD
        [mrn] => 345678
        [client_name] => hunter landseeker
        [bill_number] => bill3
        [salesforce_id] => sf3
        [michart_results] => on
        [dd_test] => Chewie
    )

)

within the main loop, I also do an inner loop on another array fed to the view called 'startList'; this one I am not having any problems with as I access items within its loop without any problem.

I think what is going on is this.

  1. If I try to show {{csvRowData.0.ward}} it will show me that value which is 'AWARD'
  2. The problem comes in where I am setting a var to be the key that I am trying to access
  3. Then twig does not seem to be able to interpret it...
  4. Using the example from #1:
  5. I do {% set gman1 = 'ward' %}
  6. I then try to access that value by doing {{csvRowData.0.gman1}} and it shows nothing (twig cannot find it or interpret it)

So that is the question, does anyone know why #1 works fine and (5 and 6) do not work at all?

2
  • Does this answer your question? Twig - Dynamic array key Commented Jan 29, 2021 at 7:52
  • You shouldn't add tags to the title... Commented Feb 2, 2021 at 18:53

1 Answer 1

0

well i tried googling again but a different question and found an answer; here is the link: Twig Access Array Index?

basically need to express it as:

csvRowData.0[gman1]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.