[英]Array of images without repetitions in Twig
我在一個目錄中有很多圖片(更多 25 張圖片)。 With a PHP function, I list sources of pictures in an array ( listImg[] ) With Symfony, I return this array with a render twig.
我想在主頁中有 9 張隨機圖片,在關於頁面中有 12 張圖片..
問題是圖片重復...
我的 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;
}
在 Twig 中:
{% for a in 1..9 %}
<img src="{{random(listImg)|imagine_filter('mini')}}"/>
{% endfor %}
我想使用 do.. 與 twig 一起使用以避免重復,但我不明白如何使用“循環”
{% for a in 1..9 %}
{{ loop.index }}
{% endfor %}
你能幫我嗎? (暫時沒有JS解決方案)
謝謝:)
我使用 shuffle(array); 返回前
shuffle($listImg);
return $listImg;
在 twig 中:
{% for a in 1..9 %}
<img src="{{listImg[a]|imagine_filter('mini')}}"/>
{% endfor %}
有用: :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.