Getting form variables data into twig templates I need to pass an array to twig along with the form.

public function buildForm(array $form, FormStateInterface $form_state) {
   $form['job_locations'] = $job_locations;
   $form['#theme'] = 'search_form';
   $form['#method'] = 'get';
   ......
   return  $form;
 }


where $job_locations is

Array
(
[0] =&gt; Array
    (
        [city] =&gt; Singapore
        [count] =&gt; 5
    )

 [1] =&gt; Array
    (
        [city] =&gt; Toronto
        [count] =&gt; 3
    )
 )


And my twig file looks like

 {% for location in form.job_locations %}
        {% if location.city  %}
          &lt;li data-original-index="0"&gt;
            &lt;a tabindex="0" class="location-list-item" data-tokens="null" role="option" aria-disabled="false" aria-selected="false"&gt;
              &lt;span class="text"&gt;{{ location.city }}&lt;/span&gt;
            &lt;/a&gt;
          &lt;/li&gt;
     {% endif %}
    {% endfor %}


Everything is coming correctly but it is throwing an error in the error log like

User error: "count" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of C:\xampp\htdocs\aws\dev\careers\core\lib\Drupal\Core\Render\Element.php) 

User error: "city" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of C:\xampp\htdocs\aws\dev\careers\core\lib\Drupal\Core\Render\Element.php) 


Please help me to solve this.
Three possible solutions:


Valid render array

You get a valid render array by putting the content of city and count in #markup elements:

  'city' =&gt; [
    '#markup' =&gt; 'Singapore',

Form variable with a #

Or start the variable name with a hash, so that it is no longer
considered to be a form element which needs to be rendered:

$form['#job_locations'] = ...


Twig:

{% for location in form['#job_locations'] %}
  ...
{% endfor %}

Separate template

Or create a custom template
with a variable for the locations array to theme the list separately from the
form:

$form['job_locations'] = [
  '#theme' =&gt; 'job_location_list',
  '#locations' =&gt; $job_locations,
];