I am going to show you how to make a simple random image generator module. You will be able to expand this to suite your needs.
First, create an image field. At the bottom of your field settings, set the repeater options. You are going to need to open a preview of the module, and set an assortment of images to the repeater to test.
The snippet for the repeater image field will look something like this:
{% for item in module.image_field %}
{% if item.src %}
<img src="{{ item.src }}" alt="{{ item.alt }}" width="{{ item.width }}" height="{{ item.height }}">
{% endif %}
{% endfor %}
You just need to strip this down a bit, and use the HubL random filter, like so:
{{ module.image_field|random }}
This is a pain to test in the module previewer, because you have to refresh the page, and refreshing the page resets the fields options. you can cause the previewer to update by adding and deleting spaces in the editor.
this filter is used to grab a random item from a group of items, it has wierd effects when used on a for loop. Use the shuffle filter instead.
Also, be aware that random means exactly that - random. Especially depending on the amount of images applied, you could end up getting the same image several times in a row.
Aug 24, 20233:36 PM - last edited on Aug 24, 20235:25 PM by Jaycee_Lewis
Member
Using random function on field group
SOLVE
No worries, we all start somewhere! It sounds like you're working on a project to display random images or on each page load using a repeater field group in Windows 11 and Edge 2022. It's great that you're trying to implement this functionality. Regarding your issue with the |random function not working as expected, let's try to troubleshoot it. The |random function should indeed return a random value from an array. Make sure that you're applying the function correctly within your loop. Here's a basic example of how you might use it... {% for item in repeaterField %} {% set randomIndex = random(0, item.values.length - 1) %} {{ item.values[randomIndex] }} {% endfor %} In this example, repeaterField is the name of your repeater field, item.values represents the array of values you have in each repeater item, and random Index calculates a random index within the range of available values. If you're still facing issues, double-check that you're using the correct syntax for the |random function. If you could share the specific code snippet you're working with, I'd be happy to help you.
I am going to show you how to make a simple random image generator module. You will be able to expand this to suite your needs.
First, create an image field. At the bottom of your field settings, set the repeater options. You are going to need to open a preview of the module, and set an assortment of images to the repeater to test.
The snippet for the repeater image field will look something like this:
{% for item in module.image_field %}
{% if item.src %}
<img src="{{ item.src }}" alt="{{ item.alt }}" width="{{ item.width }}" height="{{ item.height }}">
{% endif %}
{% endfor %}
You just need to strip this down a bit, and use the HubL random filter, like so:
{{ module.image_field|random }}
This is a pain to test in the module previewer, because you have to refresh the page, and refreshing the page resets the fields options. you can cause the previewer to update by adding and deleting spaces in the editor.
this filter is used to grab a random item from a group of items, it has wierd effects when used on a for loop. Use the shuffle filter instead.
Also, be aware that random means exactly that - random. Especially depending on the amount of images applied, you could end up getting the same image several times in a row.
The random filter works. It is designed to to grab a random item from a collection of items. it isn't meant to be used in a for loop (I will edit my previous answer):
Quick question the conditional if , is that necessary - or - is it simply checking we have a single value being returned.
Also I was finding random was working at returning a random value, but as explained I could not get the fieldname references eg item.name_of_field to work or array reference to work eg item[Index].