简体   繁体   English

遍历树枝中的多维数组

[英]Looping through multidimentional array in twig

I have created this mulidimentional array in a php model. 我已经在php模型中创建了这个数组。 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. 但是,如果我使用print_r $content数组显示所有数据,那么我知道该数组不为空。 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); 我有这个: $template->display($content);

and it needed to be this $template->display(array('content' => $content)); 它必须是这个$template->display(array('content' => $content));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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