简体   繁体   中英

Looping through multidimentional array in twig

I have created this mulidimentional array in a php model. which is below.

$handle = opendir($this->path);

while(false !== ($file = readdir($handle)))
{
    if($file != "." && $file != "..")
    {
        $this->content[] = array(
            'name' => $file,
            'name_href' => $file,
            'extn' => $this->findExts($file),
            'file_size' => number_format(filesize($this->path . '/' . $file)),
            'mod_time' => date("M j Y g:i A", filemtime($this->path . '/' . $file)),
            'time_keys' => date("YmdHis", filemtime($this->path . '/' . $file))
        );
    }
}
closedir($handle);

and I am trying to loop through it with twig. But the page does not show any content. However if I print_r the $content array all the data shows up so I know the array is not empty. Can someone help me find out how I can loop through this and display the content.

here is what I have tried

{% for key, value in content %}
    <li>
       <a class="show_content" href="{{ value.name_href }}">{{ value.name }}</a>
    </li>
{% endfor %}

but this does not display anything. And here is where I pass the content into the twig template.

$template = $twig->loadTemplate('layout.html.twig');
$template->display(array('content' => $content));

So the issue was not the loop. It was working fine.

The issue was I was not passing the content in the right way, I just was not paying attention. Silly mistake.

I had this: $template->display($content);

and it needed to be this $template->display(array('content' => $content));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM