Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

twig - Assistance with reading values in nested array

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?

question from:https://stackoverflow.com/questions/65947342/assistance-with-reading-values-in-nested-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...