0

I have array in php:

Array ( [0] => pl [1] => en )

In dataArray i have:

pageTemplate.pageHeader.pageValue.dataArray.name_pl => "Polska"
pageTemplate.pageHeader.pageValue.dataArray.name_en => "Angielska"

and twig file

{% set items = pageTemplate.pageHeader.pageValue.availableLanguageVersions %}

{% for languageVersions in items %}
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <label for="projectinput4">Name - {{ languageVersions|e|upper }}*</label>
                    <input required type="text" placeholder="Nazwa producenta" name="name_{{ languageVersions|e }}" maxlength="65" value="{{ pageTemplate.pageHeader.pageValue.dataArray.name_ }}{{ languageVersions|e }}">
                </div>
            </div>
        </div>
{% endfor %}

How can I display the value of the dataArray in html / twig in value? I would like that in name_pl be showed in value "Polska", and in name_en "Angielska".

In my current code i have in name_en => "en" and in name_pl "pl".

2
  • 3
    Possible duplicate of Twig - Dynamic array key Commented Oct 26, 2018 at 9:24
  • in the set line try changing .availableLanguageVersions to .dataArray Commented Oct 26, 2018 at 9:56

1 Answer 1

0

You should first combine the name of the attribute, then access it with the attribute twig function, as example:

{% set my_key = 'name_' ~ k %}

{{ attribute(dataArray, my_key) }}

See this runnning twig fiddle

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.