Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Archive

Can you implement a "random" feature?

Started by Muut Archive 12 years ago · 38 replies · 3245 views
12 years ago

{% 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
12 years ago

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

TWIG
{% set showcaseimage = page.media.images|randomize %}
12 years ago

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

12 years ago

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.

12 years ago

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

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

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

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

Let me know if that works.

12 years ago

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

TWIG
<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
12 years ago

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

12 years ago

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

12 years ago

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.

12 years ago

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.

12 years ago

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

12 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1277 9 years ago
Archive · by Muut Archive, 9 years ago
2 885 9 years ago
Archive · by Muut Archive, 9 years ago
2 4014 9 years ago
Archive · by Muut Archive, 9 years ago
1 2891 9 years ago
Archive · by Muut Archive, 9 years ago
3 1074 9 years ago