I had a really hard time trying to display a random image from a list of files. Normally this stuff is non-issue with plain PHP, but since because of reasons template language had to be used, a workaround had to be made.
Firstly a custom pipe function for generating a random number:
<?php
namespace Everyone;
defined('AUTOMAD') or die();
class Random {
public function Random($max, $min = 1) {
return rand($min, $max);
}
}
and then the loop. Here a randPic
variable is set, and then compared with iteration count:
<@ filelist {
glob: @{ files | def ('*.png, *.jpg') }
} @>
<@ set { :randPic: @{ :filelistCount | Everyone/Random } } @>
<@ foreach in filelist @>
<@ if @{ :i } = @{ :randPic } @>
<@ img { file: @{:file}, width: 640, height: 640, crop: true } @>
<@ end @>
<@ end @>
I was banging my head against the wall when I found a small hint here, but since there was no example code, this is what I came up with. Maybe someone'll find it useful or even improve it.