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.
- If I try to show
{{csvRowData.0.ward}}
it will show me that value which is 'AWARD'
- The problem comes in where I am setting a var to be the key that I am trying to access
- Then twig does not seem to be able to interpret it...
- Using the example from #1:
- I do
{% set gman1 = 'ward' %}
- 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?
question from:
https://stackoverflow.com/questions/65947342/assistance-with-reading-values-in-nested-array 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…