title: How to display contents of array in twig
body: <p>I've spent the last 3 days looking for a solution to this :/ </p>
  
  <p>I have a function that loops through contents of a directory:</p>
  
  <p>I was putting something together but it had a ton of for loops, so I searched online and found something similar that I was able to modify to fit what I needed:</p>
  
  <pre><code>   require_once "library/Twig/Autoloader.php";
    Twig_Autoloader::register();
  
    $loader = new Twig_Loader_Filesystem('views/');
    $twig = new Twig_Environment($loader);
  
  $dir = dirname(dirname(__FILE__)) . '/';
  
    function fileList($dir){
       global $files;
       $files = scandir($dir);
        foreach($files as $file){
            if($file != '.' &amp;&amp; $file != '..'){
                if(is_dir($dir . $file)){
                   fileList($dir . $file);
                 }
            }
        }
    };
  
    fileList($dir);
  </code></pre>
  
  <p>Then I have this:</p>
  
  <pre><code>  echo $twig-&gt;render('home.twig', array('file' =&gt; $files, 'dir' =&gt; $dir));
  </code></pre>
  
  <p>and in my home.twig file I have this:</p>
  
  <pre><code>      &lt;ol&gt;
          {% for key, files in file %}
             &lt;li&gt;{{file[key]}}&lt;/li&gt;
          {% endfor %}
        &lt;/ol&gt;
  </code></pre>
  
  <p>What I'm trying to do is display the contents on $files using twig on the page, but I cannot wrap my head around it. pls help?</p>
  
  <p>It's weird, I noticed that when I add <code>global $files;</code> inside the function it outputs only the contents of the first directory and stops. can't figure out why it stops.</p>