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 · 3250 views
12 years ago

Actually in current version there is not a good way to do this.

However, i've just commited a method github (quality()) that let's you do this for example:

TWIG
{% set blog_image = page.media.images|first.grayscale().contrast(30).quality(90).brightnes s(-100).colorize(-35,81,122) %}

You can add this method yourself to your current version. It will be part of the next release.

12 years ago

Thank you rhukster, Works like a charm :)

10 years ago

Hey, is there any possibility to set a folder, where all images are in?

10 years ago

Is this related to the topic? You want a random image from that folder?

10 years ago

You can have a user/pages/images folder, and randomize an image from that folder with page.find('/images').media.images|randomize|first

10 years ago

You can have a user/pages/images folder, and randomize an image from that folder with page.find('/images').media.images|randomize|first

Thanks for this, it works very well for the blog template. But I cannot make it work with showcase item of a modular page. It chooses one random image at first when you clear the site cache but then keeps using the same image even though randomization works on the blog page. Should I change something in the syntax to make it work with showcase items?

10 years ago

I have also tried this syntax but it does the same thing; picks a random image just at first load.

TWIG

{% set showcase_image = random(page.find('/images').media.images) %}
---
10 years ago

You could try to disable the caching in the page Advanced tab

10 years ago

The same syntax works on the blog template without disabling the caching though? I'd like to keep caching enabled.

10 years ago

I never tried the random function in a modular page, so i can't help

10 years ago

I guess this is not possible. Oh well, I think I'll need to upload images twice. One set for the showcase using the code in the first reply and one set for the blog page using the code I quoted before.

10 years ago

Hmm... Nope, I could not make this work for modular page showcase template either...

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

So what I get is, random image thing works with regular pages but not modular pages.

10 years ago

Working for me in modular pages with the

TWIG
{% set showcase_image = page.find('/images').media.images|randomize|first %}

method. The problem I'm having is only jpegs are displayed but gifs are ignored, I guess because calling

TXT
images

ignores the gif 'animated' filetype. I wonder if there's a method to call for any media, not just images.

9 years ago

I have a question... to hide some of the images in the media.images how could we do it? ext. 00.jpg 01.jpg ??

9 years ago

Maybe loop over the array first, and remove specific images, then after that call |randomize filter,

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1280 9 years ago
Archive · by Muut Archive, 9 years ago
2 888 9 years ago
Archive · by Muut Archive, 9 years ago
2 4017 9 years ago
Archive · by Muut Archive, 9 years ago
1 2892 9 years ago
Archive · by Muut Archive, 9 years ago
3 1077 9 years ago