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

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Locked
Jos
Support Guru
Support Guru
Posts: 4017
Joined: Wed Sep 05, 2007 8:03 pm
Location: The Netherlands

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

Post 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.
tyman00
Power Poster
Power Poster
Posts: 906
Joined: Tue Oct 24, 2006 5:59 pm

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

Post by tyman00 »

I like when tips are simple and effective! :) Thanks Jos!
If all else fails, use a bigger hammer.
M@rtijn wrote: This is a community. This means that we work together and have the same goal (a beautiful CMS), not that we try to put people down and make their (voluntary) job as difficult as can be.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

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

Post by Nullig »

Nice tip, Jos.

Nullig
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am
Location: Berlin

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

Post by nicmare »

:) ;) :D
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am
Location: Berlin

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

Post 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.
Jos
Support Guru
Support Guru
Posts: 4017
Joined: Wed Sep 05, 2007 8:03 pm
Location: The Netherlands

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

Post 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
giapippo
Forum Members
Forum Members
Posts: 176
Joined: Tue Feb 28, 2012 1:24 pm

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

Post 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
User avatar
Rolf
Dev Team Member
Dev Team Member
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

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

Post 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
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
giapippo
Forum Members
Forum Members
Posts: 176
Joined: Tue Feb 28, 2012 1:24 pm

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

Post 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
psy
Power Poster
Power Poster
Posts: 463
Joined: Sat Jan 22, 2005 11:19 am

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

Post 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
User avatar
Rolf
Dev Team Member
Dev Team Member
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

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

Post by Rolf »

Rolf wrote:...and add counter/break code like I added in this blog https://www.cmscanbesimple.org/blog/a-x ... ain-smarty
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
psy
Power Poster
Power Poster
Posts: 463
Joined: Sat Jan 22, 2005 11:19 am

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

Post 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.
giapippo
Forum Members
Forum Members
Posts: 176
Joined: Tue Feb 28, 2012 1:24 pm

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

Post 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
Locked

Return to “Tips and Tricks”