Page 1 of 1

Show items in a random order, shuffle your Smarty arrays!

Posted: Thu Sep 09, 2010 9:08 pm
by Jos
Many modules have ways to output lists of items. In most of them the output can be customized in "summary templates".

If you would like to display the items in a random order, some modules provide a parameter you can set. But if a module lacks a randomize parameter, there is also a simple way to accomplish this with a single line of Smarty code.

Let's look at the News module
In the summary template you'll find the Smarty array variable $items which in fact contains all the news-items. You'll also recognize the line
{foreach from=$items item=entry} which is used to loop over the $items array.

To show the newsitems in a random order you just put this code right before the {foreach from=$items item=entry} statement

Code: Select all

{capture}{$items|@shuffle}{/capture}
I used a capture around the actual shuffle function to hide the function-output '1'. Maybe there is a simpler way to do that, but this was my first thought.

Another example: Gallery
The Gallery module has a parameter action='showrandom', but if you just want the images of the specific subgallery shuffled, then you can use

Code: Select all

{capture}{$images|@shuffle}{/capture}
right before the {foreach from=$images item=image} statement

You can use this in every template where you see an array in the {foreach} or {section} tag.

Re: Show items in a random order, shuffle your Smarty arrays!

Posted: Thu Sep 09, 2010 9:14 pm
by tyman00
I like when tips are simple and effective! :) Thanks Jos!

Re: Show items in a random order, shuffle your Smarty arrays!

Posted: Thu Sep 09, 2010 10:01 pm
by Nullig
Nice tip, Jos.

Nullig

Re: Show items in a random order, shuffle your Smarty arrays!

Posted: Fri Sep 10, 2010 7:19 am
by nicmare
:) ;) :D

Re: Show items in a random order, shuffle your Smarty arrays!

Posted: Mon Sep 13, 2010 12:09 pm
by nicmare
jos, do you have an idea how to sort the foreach output by filename? maybe with a modifier for smarty? all i found was this thread in smarty forum but i have problems installing it.

Re: Show items in a random order, shuffle your Smarty arrays!

Posted: Mon Sep 13, 2010 1:43 pm
by Jos
For Gallery you can set the sorting on filename in the template settings. But I guess you're not refering to Gallery here.

In the thread you found, they try to code a custom Smarty modifier with php. That is way out of scope in this Tip&Trick

Re: Show items in a random order, shuffle your Smarty arrays

Posted: Fri Oct 10, 2014 3:36 pm
by giapippo
sorry but i have a problem

I have posted on the site about 50 news

in summary template i this code

{capture}{$items|@shuffle}{/capture}
{foreach from=$items item=entry}


in the page template i have
{news number='6' summarytemplate="carosello"}


displaying random works but always mixed the last 6 items
I would like to view 6 articles published between all 50

thanks for help

Re: Show items in a random order, shuffle your Smarty arrays

Posted: Fri Oct 10, 2014 8:29 pm
by Rolf
Remove number parameter from news tag and add counter/break code like I added in this blog https://www.cmscanbesimple.org/blog/a-x ... ain-smarty

Re: Show items in a random order, shuffle your Smarty arrays

Posted: Fri Oct 10, 2014 8:55 pm
by giapippo
thanks for replay

but i need to view max 6 random news in a page

example

http://www.teneriecoccolosi.it/news/62/ ... -sorridere

if i remove the number parameter from news tag, all news are visualized

thanks for help

Re: Show items in a random order, shuffle your Smarty arrays

Posted: Fri Oct 10, 2014 9:19 pm
by psy
I would like to view 6 articles published between all 50
I do something similar with a ListIt2 custom instance to randomly select 8 items. You will have to modify it to suit News.

Code: Select all

    {if $items|@count > 0 && shuffle($items) !== false}   
        {$items=array_slice($items,0,8)}
            <div class="row margin-top-20 services">  
    	        <!-- services -->
    	        {foreach from=$items item=item}
    	            <!-- service -->
                     ...
    	            <!-- service //-->
    	        {/foreach}
    	        <!-- services //-->
            </div>   
    {/if}        
The PHP shuffle function returns True or False so I make sure I get True returned in the {if} statement. Then I slice the array to the first 8 items.

HTH
psy

Re: Show items in a random order, shuffle your Smarty arrays

Posted: Sat Oct 11, 2014 6:12 am
by Rolf
Rolf wrote:...and add counter/break code like I added in this blog https://www.cmscanbesimple.org/blog/a-x ... ain-smarty

Re: Show items in a random order, shuffle your Smarty arrays

Posted: Sat Oct 11, 2014 7:06 am
by psy
...and add counter/break code like I added in this blog
Rolf, you don't need the counter or {break}.

After splicing the shuffled array, $items only contains the specified number of items, ie in the example above, a maximum of 8. The remainder are dropped.

No need for {capture} either when using the shuffle command in the {if} statement.

Re: Show items in a random order, shuffle your Smarty arrays

Posted: Sat Oct 11, 2014 8:17 am
by giapippo
psy wrote:
I would like to view 6 articles published between all 50
I do something similar with a ListIt2 custom instance to randomly select 8 items. You will have to modify it to suit News.

Code: Select all

    {if $items|@count > 0 && shuffle($items) !== false}   
        {$items=array_slice($items,0,8)}
            <div class="row margin-top-20 services">  
    	        <!-- services -->
    	        {foreach from=$items item=item}
    	            <!-- service -->
                     ...
    	            <!-- service //-->
    	        {/foreach}
    	        <!-- services //-->
            </div>   
    {/if}        
The PHP shuffle function returns True or False so I make sure I get True returned in the {if} statement. Then I slice the array to the first 8 items.

HTH
psy



work perfect!!!!!


{if $items|@count > 0 && shuffle($items) !== false}
{$items=array_slice($items,0,6)}

thanks thanks