[英]Array of images without repetitions in Twig
I have a lot of pictures in a directory ( more 25 pics ).我在一个目录中有很多图片(更多 25 张图片)。 With a PHP function, I list sources of pictures in an array ( listImg[] ) With Symfony, I return this array with a render twig.
With a PHP function, I list sources of pictures in an array ( listImg[] ) With Symfony, I return this array with a render twig.
I would like have 9 random pictures in Homepage and 12 pics for the About page..我想在主页中有 9 张随机图片,在关于页面中有 12 张图片..
The problem is the repetition of pictures...问题是图片重复...
My PHP function:我的 PHP function:
public function showImgDir(): array
{
$dir = "assets/img/tour";
$ext_list = array("jpg", "jpeg", "png");
$listImg = [];
$picDir= opendir($dir);
while ($file = readdir($picDir)) {
if ($file === '.' || $file === '..') {
continue;
}
$listImg[] = $dir . '/' . $file;
}
closedir($picDir);
return $listImg;
}
and in Twig:在 Twig 中:
{% for a in 1..9 %}
<img src="{{random(listImg)|imagine_filter('mini')}}"/>
{% endfor %}
I want to use do.. while with twig to avoid repetition but I don't understand how can use the 'loop'我想使用 do.. 与 twig 一起使用以避免重复,但我不明白如何使用“循环”
{% for a in 1..9 %}
{{ loop.index }}
{% endfor %}
Can you help me please?你能帮我吗? ( without JS solution for the moment )
(暂时没有JS解决方案)
Thank you:)谢谢:)
I use shuffle(array);我使用 shuffle(array); before return
返回前
shuffle($listImg);
return $listImg;
and in twig:在 twig 中:
{% for a in 1..9 %}
<img src="{{listImg[a]|imagine_filter('mini')}}"/>
{% endfor %}
it works: :)有用: :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.