Can you implement a "random" feature?

{% set showcase_image = page.media.images|first %} is working.

Can you implement {% set showcase_image = page.media.images|random %} to get a random picture in the folder ?

Thanks !

1 Like

Actually we already have that filter in our custom Twig extension, it’s called randomize:

{% set showcaseimage = page.media.images|randomize %}

oh. Sorry. I’m stupid

No problem, this is actually not documented, you would have to have looked at the code :slight_smile:

I think I am stupid … because it doesn’t work on my install : cache cleaned / restart apache … it still doesn’t work without error : the picture is just missing … but the page is generated.

Ah sorry, randomize just randomize’s the array, you then still need to grab one. So:

{% set showcaseimage = page.media.images|randomize|first %}

Another approach is to use the random function from Twig itself:

{% set showcaseimage = random(page.media.images) %}

Let me know if that works.

… does not work …

works in my testing. I have this in a test.twig.html template:

<h1>Random Test</h1>

{% set image = random(page.media.images) %}

Image: {{ image.html }}

And it outputs a random image on every page load.

Are you doing this in a template, or in a .md file???

1 Like

BTW works with |randomize|first approach also, just tested that too.

in a template

Thanks for your help

Did you get it working?

I test it as soon as possible

OK. Dont work. Perhaps I should explain :
The “random” pictures is called in template .twig file.
I would like the picture of the “showcase” part of the one-page template to be a random picture.
I have tested :
{% set showcase_image = random(page.media.images) %}
{% set showcase_image = page.media.images|randomize %}
{% set showcase_image = page.media.images|randomize|first %}

none seems to work.
Sorry. Perhaps I make a mistake

Can you show me a github gist of the whole template.html.twig file? Just paste the whole thing in there, and give me the link.

Thanks.

here it is :

Your code works fine. I just copy and pasted it into a test page and my background changed on every page load. I think you have some other issue, either setup or CSS.

  1. Do you have these images in the same folder as your page?
  2. Maybe you do and the image is loading fine, but your showcase does not have the CSS to display it properly?

Is there a working live site I can look at it see if the images are actually being loaded correctly? If you look at the source of your page, the URL in the background-image attribute should be changing.

If you just see <div class="modular-row showcase"> then Grav is not able to find the images. A quick test would be to try to display one image either directly in your content or via the twig.

is there a way to send you the URL of the beta site ?

email it to me at devs at getgrav.org

Is there a way to adjust the quality of an image? My problem is that I get big artefacts on my showcase image, caused by the automatic compression.

Thanks