title: Symfony Undefined variable (contexterror exception) when array empty in controller
body: <p>Here is the thing. A trainer can speak zero to many languages. I want to display the languages for one or several trainers on a page. Note that I'm working on a code I didn't start from scratch.</p>
  
  <p>In my controller : </p>
  
  <pre><code>foreach ($trainers as $trainer){
  $trainer_entity = $doctrine-&gt;getRepository('AppBundle:Trainer')-&gt;find($trainer["id"]);
  
   $listLangues = $this-&gt;getDoctrine()
  -&gt;getManager()
  -&gt;getRepository('AppBundle:TrainerLangues')
  -&gt;findByTrainer_id($trainer["id"]);
   }
  return [
  'user' =&gt; $user,
  'listLangues' =&gt; $listLangues,
  'trainers' =&gt; $trainers,
  ];
  </code></pre>
  
  <p>In my twig file:</p>
  
  <pre><code>{% if listLangues %}
  &lt;b&gt;Langues&lt;/b&gt; :
   {% for l in listLangues %}
           {{ l.langue.name }}
             {% endfor %}
        {% endif %}
  </code></pre>
  
  <p>This works when the person is linked to languages, so listLangues not empty. </p>
  
  <p>Otherwise I get </p>
  
  <blockquote>
    <p>Notice: Undefined variable: listLangues 500 Internal Server Error -
    ContextErrorException</p>
  </blockquote>
  
  <p>I tried <code>{% if listLangues is defined %}</code> as well, but the system seems to block in the controller. It never happened to me before.</p>
  
  <p>note that initializing <code>$listLangues</code> only leads to no display on the twig page.</p>
  
  <p>Any idea?</p>