php - Array of images without repetitions in Twig
问题描述
I have a lot of pictures in a directory ( more 25 pics ). 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..
The problem is the repetition of pictures...
My 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 :
{% 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'
{% for a in 1..9 %}
{{ loop.index }}
{% endfor %}
Can you help me please ? ( without JS solution for the moment )
解决方案
谢谢 :)
我使用 shuffle(array); 返回前
shuffle($listImg);
return $listImg;
在树枝中:
{% for a in 1..9 %}
<img src="{{listImg[a]|imagine_filter('mini')}}"/>
{% endfor %}
有用 !:)
推荐阅读
- r - 避免在 R 包 drake 中为目标保存缓存
- flutter - Flutter:有没有办法让 sliverAppBar 在按下按钮时消失
- angular - 获取父类属性列表
- r - 在 R 代码中使用嵌套的 for 循环以减少冗余
- r - R Shiny:使用点击次数命名 tabPanel
- android - 多路复用两个蓝牙设备以创建双音频
- flutter - 如何在 Flutter 中制作具有文件上传功能的聊天应用程序,例如图像、视频或 pdf
- python - 将python数据帧转换为json格式
- aws-lambda - 将两个 AWS Lambda 函数指向同一个域
- arrays - 如何对数组中的数据进行分页?